C# 移动及限制移动距离

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;

 }

}

posted @ 2017-05-19 21:56  孔琳桂  阅读(694)  评论(1编辑  收藏  举报