簡體   English   中英

Physics.Raycast有時候不會打

[英]Physics.Raycast not hit sometimes

我有一個非常奇怪的Raycast行為。 Theare是我游戲中的2個移動物體。 我在Update方法中使用raycast來查明第二個對象是否在附近。 但有時光線投射在明顯“真實”的情況下會返回虛假。 有人可以幫我解決這個問題嗎? 非常感謝!

在此輸入圖像描述

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

在unity3d中,只有在FixedUpdate()方法運行后才會更新碰撞器,這可能就是為什么你的對象沒有被光線投射擊中的原因。

通常最好在FixedUpdate()保留gameObjects與碰撞器的所有轉換,這樣光線投射應該按預期工作。

對於初學者,只需嘗試將您在問題中提到的代碼從Update()方法移動到FixedUpdate()方法(如果您沒有,只需創建一個)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM