[英]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 ();
}
}
上面的代碼已經是一個名為PlayerController
的class
在unity3d中,您可以將類拖到播放器GameObject
。 這會將腳本作為組件添加到播放器對象。 創建播放器對象時,它將為該對象創建PlayerController
的實例。
如果要設置播放器的ID,請執行以下操作:將playerId
屬性添加到PlayerController
類。 然后,當生成播放器時,可以通過使用GetComponent<PlayerController>()
訪問其PlayerController
來設置ID。您可以在生成播放器的腳本中設置ID,也可以使用一些代碼在void Start() {}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.