Unity LookAtMouse函数会影响移动性能

huangapple go评论51阅读模式
英文:

Unity LookAtMouse function kills movement performance

问题

每当我在update中调用这个函数时,它会影响我的移动。如果我不运行这个函数,一切都运行得很完美。我尝试使用transform.LookAt(),尝试在移动中使用transform.Translate(),Rigidbody.MovePosition(),更改其他任何东西都没有帮助。

如果我将transform.forward = _direction;注释掉,那么一切都完美。是什么导致我的运动变得极其缓慢?

我已经尝试了网上能找到的一切可能的方法来解决这个问题,但没有任何帮助。
英文:
private void LookAtMouse()
{
    if (Physics.Raycast(mainCamera.ScreenPointToRay(mouseLook), out var raycastHit, Mathf.Infinity))
    {
        _direction = (raycastHit.point - transform.position).normalized;
        _direction.y = 0;
        transform.forward = _direction;
    }
}

whenever I call this function in update it kills my movement. If I don't run this function everything works perfectly. I have tried using transform.LookAt() I tried using transform.Translate() in movement, Rigidbody.MovePosition(), changing anything else doesn't help.

If I comment out the transform.forward = _direction; then everything works perfect. what is it about that that makes my movement run game breakingly slow

I have tried every possible thing I can find online to fix this and nothing has been helping.

答案1

得分: 0

尝试在FixedUpdate中调用它,通常,我只在Update中用于捕获键盘输入,因为如果您的设备性能强大,它可能会被调用+1000次。另一方面,FixedUpdate更适合进行物理计算。试试看,如果没有帮助,考虑其他解决方案;

英文:

Try to call it in FixedUpdate
generally, I use Update just for catching Key input because it may be called +1000 time if your device is powerful, in the other hand fixed update is better to do the physics .iv it a try and replay if it didn't help to think in other solution;

答案2

得分: 0

当涉及物理时,您不希望通过“Transform”来设置物体,因为这会破坏物理和碰撞检测。

您应该在“FixedUpdate”中使用“Rigidbody.MoveRotation”(链接)与“Quaternion.LookRotation”(链接)相结合来实现。

英文:

As soon as any physics is involved you do not want to set things via Transform as this breaks the physics and collision detection.

You should be using Rigidbody.MoveRotation in combination with Quaternion.LookRotation within FixedUpdate

huangapple
  • 本文由 发表于 2023年2月6日 18:57:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定