栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity获取非父子关系物体的局部坐标

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity获取非父子关系物体的局部坐标

Unity有时想获取两个物体之间的相对坐标,但是又不想设置为父子关系。可以用下列代码:

void ComputeLocalTransform(Transform Father, Transform Son)
    {
        localPosition = Father.InverseTransformPoint(Son.position);
        localRotation = Quaternion.Inverse(Father.rotation) * Son.rotation;
    }

不建议将四元数改成欧拉角,我尝试过,有可能会出现位姿不一致,猜测原因可能是万向节自锁,也没去验证。
同样,通过反运算可以再求回去,由局部坐标再返回世界坐标:

void ComputeTransform(Transform Father, Transform Son)
    {
        Position = Father.TransformPoint(Son.localPosition);
        Rotation = Father.rotation * Son.localRotation;
    }

用来测试的代码也贴上来,可以自己尝试一下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestLocal : MonoBehaviour
{
    private Vector3 localPosition;
    private Vector3 localEulerAngels;
    private Quaternion localRotation;
    private Vector3 Position;
    private Vector3 EulerAngels;
    private Quaternion Rotation;
    public GameObject father;
    public GameObject son;
    private bool x,y,z;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Test();
        if (this.transform.parent != null)
        {
            ComputeLocalTransform(this.transform.parent, this.transform);
            Debug.Log("ComputeLocalTransform");
            if (localPosition != this.transform.localPosition)
            {
                Debug.LogError(this.name + "localPosition is wrong");
                Debug.LogError((localPosition - this.transform.localPosition).magnitude.ToString("f4"));
                Debug.LogError((localPosition - this.transform.localPosition).ToString("f4"));
            }

            if (localRotation != this.transform.localRotation)
            {
                Debug.LogError(this.name + "localRotation is wrong");
            }
            
            ComputeTransform(this.transform.parent, this.transform);
            Debug.Log("ComputeTransform");
            if (Position != this.transform.position)
            {
                Debug.LogError(this.name + "Position is wrong");
                Debug.LogError((Position - this.transform.position).magnitude.ToString("f4"));
                Debug.LogError((Position - this.transform.position).ToString("f4"));
            }

            if (Rotation != this.transform.rotation)
            {
                Debug.LogError(this.name + "Rotation is wrong");
            }
        }


    }

    void ComputeLocalTransform(Transform Father, Transform Son)
    {
        localPosition = Father.InverseTransformPoint(Son.position);
        localRotation = Quaternion.Inverse(Father.rotation) * Son.rotation;

    }

    void ComputeTransform(Transform Father, Transform Son)
    {
        Position = Father.TransformPoint(Son.localPosition);
        Rotation = Father.rotation * Son.localRotation;
    }

    void Test()
    {
        float speed = 5f;
        this.transform.Rotate(Vector3.right * Time.deltaTime * speed);
        this.transform.Rotate(Vector3.forward * Time.deltaTime * speed);
        this.transform.Rotate(Vector3.up * Time.deltaTime * speed);
        if(this.transform.position.x > 3)
        {
            x = false;
        }
        if (this.transform.position.x < -3)
        {
            x = true;
        }
        if (this.transform.position.y > 3)
        {
            x = false;
        }
        if (this.transform.position.y < -3)
        {
            y = true;
        }
        if (this.transform.position.z > 3)
        {
            z = false;
        }
        if (this.transform.position.z < -3)
        {
            z = true;
        }
        if(x)
        {
            this.transform.position = new Vector3(this.transform.position.x + 0.6f * Time.deltaTime, transform.position.y, transform.position.z);
        }
        else
        {
            this.transform.position = new Vector3(this.transform.position.x - 0.6f * Time.deltaTime, transform.position.y, transform.position.z);
        }
        if (y)
        {
            this.transform.position = new Vector3(this.transform.position.x, transform.position.y + 0.6f * Time.deltaTime, transform.position.z);
        }
        else
        {
            this.transform.position = new Vector3(this.transform.position.x, transform.position.y - 0.6f * Time.deltaTime, transform.position.z);
        }
        if (z)
        {
            this.transform.position = new Vector3(this.transform.position.x , transform.position.y, transform.position.z + 0.6f * Time.deltaTime);
        }
        else
        {
            this.transform.position = new Vector3(this.transform.position.x, transform.position.y , transform.position.z - 0.6f * Time.deltaTime);
        }
    }
}

不知道为什么运行一段时间后local Position会报错,但是Debug出来显示距离其实没有没有差别,可能是精度的问题吧。

四元数虽然比较难理解,但是是个好东西,建议大家以后表示位姿都用四元数。
如果有错误,请大家指正。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900678.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号