英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论