簡體   English   中英

如何為我的播放器腳本統一創建一個類?

[英]How do I create a class for my player script in unity?

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 ();
        }

}

上面的代碼已經是一個名為PlayerControllerclass

在unity3d中,您可以將類拖到播放器GameObject 這會將腳本作為組件添加到播放器對象。 創建播放器對象時,它將為該對象創建PlayerController的實例。

如果要設置播放器的ID,請執行以下操作:將playerId屬性添加到PlayerController類。 然后,當生成播放器時,可以通過使用GetComponent<PlayerController>()訪問其PlayerController來設置ID。您可以在生成播放器的腳本中設置ID,也可以使用一些代碼在void Start() {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM