福感科技有限公司 欢迎您!
联系方式

    地址:北京市平谷区马坊镇金河北街17号院3号楼7层712

    电话:010-89968230

    网站:http://www.fgsense.com

U3d中如何计算角度偏移

2022-9-2 17:24:57      点击:

U3d角度偏移计算方法:


using UnityEngine;
using System.Collections;
 
public class UserAnimatorMove : MonoBehaviour
{
    private Vector3 oldVector3;
    private Vector3 newVector3;
    private Quaternion oldQuaternion;
    private Vector3 newRotatinForward;
    void Start()
    {

    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1))
        {
 
            oldVector3 = this.transform.position;
            oldQuaternion = this.transform.rotation;
        }
        if (Input.GetKeyDown(KeyCode.F2))
        {
            StartCoroutine(MovePoint());
        }
    }
    private IEnumerator MovePoint()
    {
        //    oldVector3 = this.transform.position;
        //   oldQuaternion = this.transform.rotation;
        yield return new WaitForSeconds(0.2f);
        newVector3 = this.transform.position;
        newRotatinForward = this.transform.forward;
 
        float lenth = Vector3.Distance(oldVector3, newVector3);
        Vector3 velocity = Quaternion.Inverse(oldQuaternion) * newRotatinForward;
        float angle = Mathf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f;
        Debug.Log(angle);//新的方向相对旧的方向的偏转角度
    }
}

Copyright 2019 www.fgsense.com

福感科技有限公司 版权所有 All Rights Reserved

京ICP备20002031号

010-89968230