zoukankan      html  css  js  c++  java
  • unity用刚体做玩家移动和玩家看向鼠标点

     1  //人物移动的一些基础属性
     2     private float speed = 5;
     3     private float h, v;
     4     private Rigidbody rig;
     5     private Camera cam;
     6     private Animator anm;
     7     private bool isMove=false;
     8     // Use this for initialization
     9     void Start () {
    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     void Update () {
    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 = new Vector3(hit.point.x, transform.position.y, hit.point.z);
    43             lookat = lookat - transform.position;
    44            //旋转目标朝向
    45             transform.rotation = Quaternion.LookRotation(lookat);
    46         }
  • 相关阅读:
    读取xml文件(可执行文件根目录debug)
    c# winform textbox与combox让用户不能输入
    枚举类型
    值类型与引用类型
    error: failed to push some refs to 'https://git.oschina.net/bluede/TuShuGuanLi.g it'
    left join on 和where中条件的放置位置
    left join、right join、inner join、full join
    Union、Union All、Intersect、Minus
    分层设计的好处
    Hibernate查询方式
  • 原文地址:https://www.cnblogs.com/mafeihao/p/10155001.html
Copyright © 2011-2022 走看看