1. 程式人生 > >C# 移動及限制移動距離

C# 移動及限制移動距離

tor transform called nbsp mono initial delta atime osi

public class PlaneMove : MonoBehaviour {

//h:水平方向的控制,v:前後方向的控制

float h,v;

//Speed:飛機的飛行速度

public float Speed;

// Use this for initialization

void Start () { }

// Update is called once per frame

void Update () {

//水平方向的值

h = Input.GetAxis("Horizontal");

//垂直方向的值

v = Input.GetAxis("Vertical");

//垂直方向每秒移動一米

transform.position +=-v * new Vector3(0,0,1f) * Speed * Time.deltaTime;

//水平方向每秒移動一米

transform.position +=-h * new Vector3(1f,0,0) * Speed * Time.deltaTime;

//限制坐標 x軸 -3.5~3.5,z軸-10~1

Vector3 pos = transform.position;

float x = pos.x;

float z = pos.z;

x = Mathf.Clamp(x, -3.5f, 3.5f);

z = Mathf.Clamp(z, -10f, 1f);

pos.Set(x, pos.y, z);

transform.position = pos;

}

}

C# 移動及限制移動距離