可以同步子和父母职位
我有一个父母和一个子对象。当我移动子对象时,我想父母将与孩子相同的位置移至相同的位置。我试图通过在父对象上使用此脚本来实现这一目标:
private void Update()
{
Vector3 childPos = transform.GetChild(0).position;
transform.position = childPos;
transform.GetChild( 0 ).position = childPos;
}
因此,将父母的世界空间位置设置为孩子的世界空间位置,然后将孩子的世界空间位置设置为在父母移动之前的位置。这是影响任何一个对象位置的唯一脚本。
然后,当我在编辑器中移动子对象时,会产生这种怪异的laggy效果,使对象无法正确遵循光标。这是一个gif显示效果: https://gfycat.com/negatigativevigorforcatcicada
我得到以下显示的日志以某种方式,孩子的世界空间位置正在设置的框架末端和下一帧的开始之间发生变化: 在这里屏幕截图
我在想,也许孩子的转换可能会在与父母的某个点上再次更新,但是我尝试在孩子上的脚本中设置新位置,并在孩子的脚本中设置新位置,问题仍然发生了。移动父母后,将孩子的本地位置设置为0,0,0。我尝试将两个对象设置为单独的对象的位置并将其移动,这并没有导致滞后发生,因此绝对与育儿有关。
关于为什么发生这种情况的任何解释或想法将不胜感激!
I've got a parent and a child object. When I move the child object I want to parent to move to the same position as the child. I'm trying to achieve this with this script on the parent object:
private void Update()
{
Vector3 childPos = transform.GetChild(0).position;
transform.position = childPos;
transform.GetChild( 0 ).position = childPos;
}
So, set the parent's world space position to the child's world space position and then set the child's world space position to what it was before its parent moved. This is the only script affecting the position of either object.
When I then move the child object in the editor it does this weird laggy effect where the object doesn't properly follow the cursor. Here's a gif showing the effect: https://gfycat.com/negativevigorouscicada
I got the following logs which show that somehow the child's world space position is being changed between the end of the frame where it's been set and the start of the next frame:
Screenshot here
I was thinking maybe the child's transform gets updated again at some point relative to its parent, but I tried setting the new position in LateUpdate in a script on the child and the problem still happened. Setting the child's local position to 0,0,0 after moving the parent doesn't work either. I tried setting both objects to the position of a separate object and moving that, which didn't cause the lag to happen so it's definitely something to do with the parenting.
Any explanations or ideas as to why this is happening would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论