简体   繁体   中英

Physics.Raycast not hit sometimes

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);

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM