Unity中lineOfSight射线未触发目标Debug.Log但wayPointFinder射线却触发的问题咨询
Unity中lineOfSight射线未触发目标Debug.Log但wayPointFinder射线却触发的问题咨询
大家好,我在Unity项目里实现了一个GoToWayPoint函数,里面用到了多条射线检测,现在碰到一个很诡异的问题:lineOfSight这条射线对应的Debug日志完全没按预期触发,反而wayPointFinder射线会莫名其妙触发这条日志?而且RaycastHit lineOfSight的写法和另一条正常工作的borderFinder射线几乎一样,我实在排查不出问题所在。
具体细节:
lineOfSight射线里有一句调试代码:Debug.Log("isBorder " + isBorder + " objectName: " + hitObjectName);,但实际运行时,这条日志是被wayPointFinder射线触发的,而非lineOfSight自身- 另外,用于检测左右传感器的
borderFinder射线是能正常识别到带trackBorder标签的碰撞体的
下面是完整的GoToWayPoint函数代码:
public void GoToWayPoint() { // uses Rigidbody.MoveRotation HRLSpeedController speedController = gameObject.GetComponent<HRLSpeedController>(); // Check if there are any waypoints in the array if (npcWayPoints.Length == 0) return; // Get the current waypoint GameObject currentWaypoint = npcWayPoints[currentWaypointIndex]; // Find the GameObject with the "trackBorder" tag GameObject trackBorder = GameObject.FindGameObjectWithTag("trackBorder"); RaycastHit wayPointFinder; Physics.Raycast(sensorWayPointFinder.transform.position, sensorWayPointFinder.transform.forward, out wayPointFinder, rayWayPointDistance); Debug.DrawRay(sensorWayPointFinder.transform.position, sensorWayPointFinder.transform.forward * rayWayPointDistance, new Color(1f, 0.65f, 0f)); //Debug.Log("currentWaypointIndex: " + currentWaypointIndex + " currentVertexIndex: " + currentVertexIndex); if (isTurning) { // Get the current vertex GameObject currentVertex = vertex[currentVertexIndex]; if (wayPointFinder.collider != null && wayPointFinder.collider.gameObject == currentVertex) { currentVertexIndex++; if (currentVertexIndex >= vertex.Length) { currentVertexIndex = 0; } currentVertex = vertex[currentVertexIndex]; } // Calculate the direction from rbFuselage to currentVertex Vector3 direction = (currentVertex.transform.position - rbFuselage.position).normalized; // Calculate the target rotation Quaternion targetRotation = Quaternion.LookRotation(direction); // Interpolate between the current rotation of rbFuselage and targetRotation using Slerp rbFuselage.rotation = Quaternion.Slerp(rbFuselage.rotation, targetRotation, Time.deltaTime * rbFuselageRotationSpeed); } else { if (wayPointFinder.collider != null && wayPointFinder.collider.gameObject == currentWaypoint) { currentWaypointIndex++; if (currentWaypointIndex >= npcWayPoints.Length) { currentWaypointIndex = 0; } currentWaypoint = npcWayPoints[currentWaypointIndex]; } // Calculate the direction from rbFuselage to currentWaypoint Vector3 direction = (currentWaypoint.transform.position - rbFuselage.position).normalized; // Calculate the target rotation Quaternion targetRotation = Quaternion.LookRotation(direction); // Interpolate between the current rotation of rbFuselage and targetRotation using Slerp rbFuselage.rotation = Quaternion.Slerp(rbFuselage.rotation, targetRotation, Time.deltaTime * rbFuselageRotationSpeed); }/**/ #region borderFinder: uses rayCast to check for trackBorder & strafes left/right if (isTrackborder) // lateral ray border check RaycastHit borderFinder; Physics.Raycast(sensorFRight.transform.position, sensorFRight.transform.forward, out borderFinder, rayBorderDistance); Debug.DrawRay(sensorFRight.transform.position, sensorFRight.transform.forward * rayBorderDistance, new Color(1f, 0.65f, 0f)); if (borderFinder.collider != null && borderFinder.collider.gameObject.CompareTag("trackBorder")) { //RIGHT hit move rbFuselage left Debug.Log("sensorFrRight Hit trackBorder"); engineControl.NPCStrafeLeft(pushOffWallForce++); } else { // Reset pushOffWallForce to its initial value pushOffWallForce = initialPushOffWallForce; //Debug.Log(pushOffWallForce); } Physics.Raycast(sensorFLeft.transform.position, sensorFLeft.transform.forward, out borderFinder, rayBorderDistance); Debug.DrawRay(sensorFLeft.transform.position, sensorFLeft.transform.forward * rayBorderDistance, new Color(1f, 0.65f, 0f)); if (borderFinder.collider != null && borderFinder.collider.gameObject.CompareTag("trackBorder")) { //LEFT hit move rbFuselage right Debug.Log("sensorFrLeft Hit trackBorder"); engineControl.NPCStrafeRight(pushOffWallForce++); } else { // Reset pushOffWallForce to its initial value pushOffWallForce = initialPushOffWallForce; //Debug.Log(pushOffWallForce); } #endregion #region lineOfSight RaycastHit lineOfSight; Physics.Raycast(sensorBorderFinder.transform.position, sensorBorderFinder.transform.forward, out lineOfSight, rayLineOfSightDistance); Debug.DrawRay(sensorBorderFinder.transform.position, sensorBorderFinder.transform.forward * rayLineOfSightDistance, new Color(1f, .25f, 0f)); string hitObjectName = ""; //Debug.Log("lineOfSight"); //if (borderFinder.collider != null && borderFinder.collider.gameObject.CompareTag("trackBorder")) if (lineOfSight.collider != null && lineOfSight.collider.gameObject.CompareTag("trackBorder")) { isBorder = true; hitObjectName = lineOfSight.collider.gameObject.name; Debug.Log("isBorder " + isBorder + " objectName: " + hitObjectName); } #endregion engineControl.NPCAccelerate(npcThrustInput); }
备注:内容来源于stack exchange,提问作者junji
相关产品推荐
相关产品推荐

