1 //人物移动的一些基础属性 2 private float speed = 5; 3 private floath, v; 4 privateRigidbody rig; 5 privateCamera cam; 6 privateAnimator anm; 7 private bool isMove=false; 8 //Use this for initialization 9 voidStart () { 10 11 rig = gameObject.GetComponent<Rigidbody>();//寻找刚体组件 12 cam =Camera.main; //设置主摄像机 13 anm = gameObject.GetComponent<Animator>();//动画组件 14 } 15 16 //Update is called once per frame 17 voidUpdate () { 18 //人物移动 19 h = Input.GetAxisRaw("Horizontal"); //寻找对应的轴 20 v = Input.GetAxisRaw("Vertical"); 21 Vector3 v3 = new Vector3(h, 0, v).normalized; 22 rig.velocity = v3 * speed;//给刚体施加一个力。 因为是施加力的原因 所以要把旋转给锁定 防止 物体翻滚。 23 rig.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationY |RigidbodyConstraints.FreezePositionY; 24 if (h == 0 && v == 0) 25 { 26 isMove = false;//这是动画系列 可以不用考虑 27 anm.SetBool("Move", false); 28 } 29 else 30 { 31 isMove = true; 32 anm.SetBool("Move", true); 33 } 34 35 36 37 //人物朝向鼠标点击的方向 38 Ray ray =cam.ScreenPointToRay(Input.mousePosition); 39 RaycastHit hit; 40 if (Physics.Raycast (ray ,out hit,1<<8)) 41 { 42 Vector3 lookat = newVector3(hit.point.x, transform.position.y, hit.point.z); 43 lookat = lookat -transform.position; 44 //旋转目标朝向 45 transform.rotation =Quaternion.LookRotation(lookat); 46 }
unity用刚体做玩家移动和玩家看向鼠标点
免责声明:文章转载自《unity用刚体做玩家移动和玩家看向鼠标点》仅用于学习参考。如对内容有疑问,请及时联系本站处理。
上篇ADO.NET基础必备之SqlParameterCollection 类mysql存储过程返回查询结果集下篇
宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=