如何统一为我的播放器脚本创建一个类?

时间:2016-04-07 01:37:31

标签: c# unity3d

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{


    public float speed = 80.0f; // Code for how fast the ball can move. Also it will be public so we can change it inside of Unity itself. 
    private Rigidbody rb; // Variable that applies itself to Ball 


    // Code That enables our Player to move around. 
    void Movement()
    {

        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rb.AddForce (movement * speed);
    }


    // Update is called once per frame
    void Update ()
        {
            rb = GetComponent<Rigidbody> ();
            Movement ();
        }

}

1 个答案:

答案 0 :(得分:1)

上面的代码已经是class,名为PlayerController

在unity3d中,您可以将课程拖到播放器GameObject上。这会将脚本作为组件添加到您的播放器对象中。创建播放器对象后,它将为该对象创建PlayerController的实例。

如果您想为播放器设置ID: 在playerId课程中添加PlayerController媒体资源。然后,当你产生播放器时,你可以通过使用PlayerController访问它GetComponent<PlayerController>()来设置ID。您可以在产生播放器的脚本中设置ID,或者您可以使用一些代码来执行此操作在void Start() {}