Physics.Raycast not hit sometimes

人盡茶涼 提交于 2019-12-13 04:50:30

问题


I have a very strange Raycast behaviour. Theare are 2 moving objects in my game. I use raycast in the Update method to find out if the second object is near. But sometimes raycast return false in obviously "true" situations. Can somebody help me to fix this issue? Thanks a lot!

    // Returns false, but should be true
    var middle = Physics.Raycast(Car.SensorPointRight.position, 
                                 Car.CarObject.right, out middleHitsInfo, 
                                (DistanceBetweenPaths - _carColliderOffset));

    if (IsUserCar)
        DebugHepler.Ray(Car.SensorPointRight.position, 
                        Car.CarObject.right * (DistanceBetweenPaths - _carColliderOffset),
                        middle ? Color.red : Color.white);

回答1:


In unity3d, colliders are updated only after the FixedUpdate() method runs, so that might be why your objects aren't being hit by the raycast.

It's usually better to keep all transformations of gameObjects with colliders in FixedUpdate(), that way the raycast should work as expected.

For starters simply try moving the code you mentioned in your question from the Update() method to the FixedUpdate() method (If you don't have one, simply create one).



来源:https://stackoverflow.com/questions/20458248/physics-raycast-not-hit-sometimes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!