码迷,mamicode.com
首页 > 移动开发 > 详细

C# 移动及限制移动距离

时间:2017-05-20 00:02:22      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:oat   cal   public   mon   star   控制   transform   坐标   nsf   

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# 移动及限制移动距离

标签:oat   cal   public   mon   star   控制   transform   坐标   nsf   

原文地址:http://www.cnblogs.com/C-9925/p/6880421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!