Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unity] 如何记录位置和旋转变化前后的差值及还原 #28

Open
yangruihan opened this issue Dec 24, 2020 · 0 comments
Open

Comments

@yangruihan
Copy link
Owner

yangruihan commented Dec 24, 2020

[Unity] 如何记录位置和旋转变化前后的差值及还原

背景介绍

当下遇到一个问题,通过记录位置差值(PositionOffset)和旋转差值(RotationOffset),通过当前位置和旋转,计算出新的位置和旋转

差值通过另一个 GameObject(Target) 的位置和旋转与当前 GameObject(Self) 计算得出

解决方案

记录

位置差值:

PositionOffset = Self.transform.worldToLocalMatrix *
                       (Target.transform.position - Self.transform.position);

旋转差值:

RotationOffset = Quaternion.Inverse(Self.transform.rotation) * Target.transform.rotation; // 四元数差值

还原

位置:

Self.transform.position + PositionOffset;

旋转:

Self.transform.rotation * RotationOffset;

参考链接

https://stackoverflow.com/questions/50428971/unity3d-relative-offset-object-rotation-using-quaternions

@yangruihan yangruihan changed the title [Unity] 如何记录变化前和变化后角度的变化量(即四元数的差值) [Unity] 如何记录位置和旋转变化前后的差值及还原 Dec 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant