Transform

作者: 叫我颜先生 | 来源:发表于2022-04-06 20:50 被阅读0次

    简介

    基本API用法以及一些注意事项


    position && rotation
    • 不会检测碰撞
        //插值计算
        void Update()
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
    
            if (Mathf.Approximately(h, 0) && Mathf.Approximately(v, 0))
                return;
    
            Vector3 dir = new Vector3(h, 0, v).normalized;
    
            transform.position = Vector3.Lerp(transform.position, transform.position + dir, Time.deltaTime * movespeed);
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), Time.deltaTime * rotatespeed);
        }
    

    CharacterController
    • 角色控制器,检测碰撞
        void Update()
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
    
            if (Mathf.Approximately(h, 0) && Mathf.Approximately(v, 0))
                return;
    
            Vector3 dir = new Vector3(h, 0, v).normalized;
    
            cc.SimpleMove(dir * movespeed);
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), Time.deltaTime * rotatespeed);
        }
    

    Translate && Rotate

    Translate 默认Space.Self

    • Translate(Vector3.forward, Space.World) //世界坐标系中,沿着世界正前方方向
    • Translate(transform.forward, Space.World) //世界坐标系中,沿着自身正前方
    • Translate(Vector3.forward, Space.Self) //自身坐标系中,沿着世界坐标的正前方相对自身坐标系的方向,和上方相同效果
    • Translate(transform.forward, Space.Self) //不正常,自身正前方的向量在相对世界中的那个向量,转化到在自身坐标系的最终方向

    Rotate同样可以按照自身或者世界坐标系旋转

        void Update()
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
    
            if (Mathf.Approximately(h, 0) && Mathf.Approximately(v, 0))
                return;
    
            Vector3 dir = new Vector3(h, 0, v).normalized;
    
            transform.Translate(dir * movespeed, Space.World);
            //transform.Rotate(Vector3.up * rotatespeed, Space.World);
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), Time.deltaTime * rotatespeed);
        }
    

    Rigidbody
    • 检测碰撞
        void Update()
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
    
            if (Mathf.Approximately(h, 0) && Mathf.Approximately(v, 0))
                return;
    
            Vector3 dir = new Vector3(h, 0, v).normalized;
    
            //rigidbody.AddForce(dir * movespeed, ForceMode.Force);
    
            rigidbody.MovePosition(transform.position+dir*movespeed);
            rigidbody.MoveRotation(Quaternion.LookRotation(dir));
        }
    

    Vector3 && Quaternion
        Vector3.Angle(Vector3, Vector3);                //向量夹角
        Vector3.ClampMagnitude(Vector3, maxLength);     //复制一个向量,指定最大长度
    
        Vector3.Cross(Vector3, Vector3);                //差乘(右手大拇指确定方向)
        Vector3.Dot(Vector3, Vector3);                  //点乘(大于0小于0)
    
        Vector3.Distance(Vector3, Vector3);             //距离
        Vector3.Project(Vector3, Vector3);              //投影
        Vector3.Reflect(Vector3, Vector3);              //反射向量
    
        Quaternion.Angle(a, b);                     //夹角
        Quaternion.AngleAxis(angle, axis);          //绕轴转角度
        Quaternion.Dot(a, b);
        Quaternion.Euler(euler);
        Quaternion.FromToRotation(from, to);
        Quaternion.Inverse(a);                      //反向
        Quaternion.LookRotation(v3);                //看向
        Quaternion.RotateTowards(from, to, angle);  //转向
        Quaternion.Slerp(a, b, t);
    

    LookRotation:看向一个向量,是一个方向
    LookAt:看向一个坐标,一个点

    相关文章

      网友评论

          本文标题:Transform

          本文链接:https://www.haomeiwen.com/subject/rcwvyhtx.html