gpt4 book ai didi

unity-game-engine - 如何将一个对象粘到另一个对象上并返回到 Unity 中的起点

转载 作者:行者123 更新时间:2023-12-01 16:30:19 25 4
gpt4 key购买 nike

我想用另一个对象捕获一个对象。然后将其带到第一个对象的起点(2D)。我以恒定速度将一个物体扔向另一个物体。第一个物体与第二个物体发生碰撞,之后我定义了第一个物体相对于站立的第二个物体的反向速度。当发生碰撞时(onCollisonListener)它不会自然回头,所以回头的角度是错误的。

他们随机去任何地方。我怎样才能使折返功能正常? 我可以使用哪个函数?我只使用velocity.x和velocity.y感谢您阅读我的问题并提供帮助,此致。

最佳答案

伪代码:

public class Catcher : MonoBehaviour
{
Vector2 startingPoint;

void Awake()
{
startingPoint = transform.position;
}

void Update()
{
//Your moving code
}

void OnTriggerEnter(Collider other)
{
if(other.tag == "someTag")
{
other.transform.parent = transform;
StartCoroutine("MoveBack");
}
}

private IEnumerator MoveBack()
{
while(transform.position != startingPoint)
{
// Move this towards starting point
// Use Vector2 functions like Lerp, MoveTowards or SmoothDamp
// Could also use Mathf functions
yield null;
}
}
}

也许可以添加一个标志/检查来了解对象是否向后或向前移动。

关于unity-game-engine - 如何将一个对象粘到另一个对象上并返回到 Unity 中的起点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29670394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com