protected Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) { // RaycastHit[] hits = Physics.RaycastAll(ray); colliders = transform.root.gameObject.GetComponentsInChildren <PhysXCollider>(); Transform closestHit = null; float distance = 0; hitPoint = Vector3.zero; PhysXRaycastHit hitPhysX = PhysXRaycast.GetRaycastHit(); if (PhysXRaycast.Fire(ray.origin, ray.direction, hitPhysX, 9999, raycastLayers, rigidbodyVehicleId)) { closestHit = hitPhysX.transform; hitPoint = hitPhysX.point; } PhysXRaycast.ReleaseRaycastHit(hitPhysX); // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit return(closestHit); }
protected Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) { Transform closestHit = null; hitPoint = Vector3.zero; PhysXRaycastHit hitPhysX = PhysXRaycast.GetRaycastHit(); if (PhysXRaycast.Fire(ray.origin, ray.direction, hitPhysX, raycastLayers, rigidbodyVehicleId)) { closestHit = hitPhysX.transform; hitPoint = hitPhysX.point; } PhysXRaycast.ReleaseRaycastHit(hitPhysX); return(closestHit); }
protected RaycastHitDetails FindClosestRaycastHitDetails(Ray ray, Vector3 targetPoint) { RaycastHitDetails raycastHitDetails = new RaycastHitDetails(targetPoint, Vector3.zero, null, false, false);; //RaycastHit[] hits = Physics.RaycastAll(ray); // if (PhysXRaycast.Fire(sensorStartPos, transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { Transform closestHit = null; float distance = 0; Vector3 hitPoint = Vector3.zero; PhysXRaycastHit hitPhysX = PhysXRaycast.GetRaycastHit(); if (PhysXRaycast.Fire(ray.origin, ray.direction, hitPhysX, 999, raycastLayers, rigidbodyVehicleId)) { closestHit = hitPhysX.transform; distance = hitPhysX.distance; hitPoint = hitPhysX.point; // get local hitpoint Vector3 localHitPoint = closestHit.root.InverseTransformPoint(hitPoint); // the health script exists if (hitPhysX.transform.root.GetComponent <VehicleHealthManager>() != null) { raycastHitDetails = new RaycastHitDetails(hitPoint, localHitPoint, closestHit, true, true); } else { raycastHitDetails = new RaycastHitDetails(hitPoint, localHitPoint, closestHit, false, true); } } PhysXRaycast.ReleaseRaycastHit(hitPhysX); return(raycastHitDetails); }
bool SensorsManouvre() { bool avoiding = false; // RaycastHit hit; Vector3 sensorStartPos = transform.position; sensorStartPos += transform.forward * frontSensorPosition.z; sensorStartPos += transform.up * frontSensorPosition.y; float avoidMultiplier = 0; avoiding = false; PhysXRaycastHit hit = PhysXRaycast.GetRaycastHit(); //front right sensor sensorStartPos += transform.right * frontSideSensorPosition; if (PhysXRaycast.Fire(sensorStartPos, transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { Debug.DrawLine(sensorStartPos, hit.point); avoiding = true; avoidMultiplier -= 1f; } //front right angle sensor else if (PhysXRaycast.Fire(sensorStartPos, Quaternion.AngleAxis(frontSensorAngle, transform.up) * transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { Debug.DrawLine(sensorStartPos, hit.point); avoiding = true; avoidMultiplier -= 0.5f; } //front left sensor sensorStartPos -= transform.right * (frontSideSensorPosition * 2); if (PhysXRaycast.Fire(sensorStartPos, transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { Debug.DrawLine(sensorStartPos, hit.point); avoiding = true; avoidMultiplier += 1f; } //front left angle sensor else if (PhysXRaycast.Fire(sensorStartPos, Quaternion.AngleAxis(-frontSensorAngle, transform.up) * transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { Debug.DrawLine(sensorStartPos, hit.point); avoiding = true; avoidMultiplier += 0.5f; } //front center sensor if (avoidMultiplier == 0) { if (PhysXRaycast.Fire(sensorStartPos, transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { if (!terrainTags.Contains(hit.collider.tag)) { Debug.DrawLine(sensorStartPos, hit.point); avoiding = true; if (hit.normal.x < 0) { avoidMultiplier = -1; } else { avoidMultiplier = 1; } } } } if (avoiding) { CarDriver.Steer(avoidMultiplier); if (avoidMultiplier >= 1 && myRb.velocity.magnitude > maxSpeed / 2) { CarDriver.Reverse(); } else if (avoidMultiplier <= -1 && myRb.velocity.magnitude > maxSpeed / 2) { CarDriver.Reverse(); } } return(avoiding); }
void CalculateSensors() { //Debug.Log("player layer: " + playerLayer); float leftRightCoefficient = 0; bool avoiding = false; bool playerAhead = false; Vector3 sensorStartPos = transform.position; sensorStartPos += transform.forward * frontSensorPosition.z; sensorStartPos += transform.up * frontSensorPosition.y; float crashSensorValue = 0; float telecastCrashSensorValue = 0; distList.Clear(); timeList.Clear(); // calculate a value of crashing // Vector3 targetVel = Vector3.zero; //front right sensor sensorStartPos += transform.right * frontSideSensorPosition; PhysXRaycastHit hit = PhysXRaycast.GetRaycastHit(); if (PhysXRaycast.Fire(sensorStartPos, transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { if (drawDebugLines) { Debug.DrawLine(sensorStartPos, hit.point); } avoiding = true; float increase = 1f; float telecastIncrease = 1f; // if normal is > angle if (Vector3.Angle(hit.normal, transform.up) > crashAngleThreshold && Vector3.Dot(hit.normal, Vector3.up) < dotThreshold) { if (hit.collider.gameObject.layer == playerLayer) { telecastIncrease *= playerSensorMultiplier; playerAhead = true; CalculateTimeToHit(hit.collider.attachedRigidBody); } else { CalculateTimeToHit(hit.point); } crashSensorValue += increase; telecastCrashSensorValue += telecastIncrease; leftRightCoefficient += 0.75f; } else { distList.Add(myRb.velocity.magnitude * sensorLength); timeList.Add(1f); } } //front right angle sensor else if (PhysXRaycast.Fire(sensorStartPos, Quaternion.AngleAxis(frontSensorAngle, transform.up) * transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { if (drawDebugLines) { Debug.DrawLine(sensorStartPos, hit.point); } float increase = 0.5f; float telecastIncrease = 0.5f; if (Vector3.Angle(hit.normal, transform.up) > crashAngleThreshold * 0.75 && Vector3.Dot(hit.normal, Vector3.up) < dotThreshold) { if (hit.collider.gameObject.layer == playerLayer) { telecastIncrease *= playerSensorMultiplier; playerAhead = true; CalculateTimeToHit(hit.collider.attachedRigidBody); } else { CalculateTimeToHit(hit.point); } crashSensorValue += increase; telecastCrashSensorValue += telecastIncrease; leftRightCoefficient += 0.25f; } else { distList.Add(myRb.velocity.magnitude * sensorLength); timeList.Add(1f); } } //front left sensor sensorStartPos -= transform.right * (frontSideSensorPosition * 2); if (PhysXRaycast.Fire(sensorStartPos, transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { if (drawDebugLines) { Debug.DrawLine(sensorStartPos, hit.point); } float increase = 1f; float telecastIncrease = 1f; if (Vector3.Angle(hit.normal, transform.up) > crashAngleThreshold && Vector3.Dot(hit.normal, Vector3.up) < dotThreshold) { if (hit.collider.gameObject.layer == playerLayer) { telecastIncrease *= playerSensorMultiplier; playerAhead = true; CalculateTimeToHit(hit.collider.attachedRigidBody); } else { CalculateTimeToHit(hit.point); } crashSensorValue += increase; telecastCrashSensorValue += telecastIncrease; leftRightCoefficient -= 0.75f; } else { distList.Add(myRb.velocity.magnitude * sensorLength); timeList.Add(1f); } } //front left angle sensor else if (PhysXRaycast.Fire(sensorStartPos, Quaternion.AngleAxis(-frontSensorAngle, transform.up) * transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { if (drawDebugLines) { Debug.DrawLine(sensorStartPos, hit.point); } float increase = 0.5f; float telecastIncrease = 0.5f; if (Vector3.Angle(hit.normal, transform.up) > crashAngleThreshold * 0.75 && Vector3.Dot(hit.normal, Vector3.up) < dotThreshold) { if (hit.collider.gameObject.layer == playerLayer) { telecastIncrease *= playerSensorMultiplier; playerAhead = true; CalculateTimeToHit(hit.collider.attachedRigidBody); } else { CalculateTimeToHit(hit.point); } crashSensorValue += increase; telecastCrashSensorValue += telecastIncrease; leftRightCoefficient -= 0.25f; } else { distList.Add(myRb.velocity.magnitude * sensorLength); timeList.Add(1f); } } //front center sensor if (PhysXRaycast.Fire(sensorStartPos, transform.forward, hit, sensorLength, sensorLayerMask, myRb.vehicleId)) { if (drawDebugLines) { Debug.DrawLine(sensorStartPos, hit.point); } float increase = 1.5f; float telecastIncrease = 1.5f; if (Vector3.Angle(hit.normal, transform.up) > crashAngleThreshold && Vector3.Dot(hit.normal, Vector3.up) < dotThreshold) { if (hit.collider.gameObject.layer == playerLayer) { telecastIncrease *= playerSensorMultiplier; playerAhead = true; // Debug.Log("hit collider is: " + hit.collider + " and rb is: " + hit.collider.attachedRigidBody); CalculateTimeToHit(hit.collider.attachedRigidBody); } else { CalculateTimeToHit(hit.point); } crashSensorValue += increase; telecastCrashSensorValue += telecastIncrease; } else { distList.Add(myRb.velocity.magnitude * sensorLength); timeList.Add(1f); } } PhysXRaycast.ReleaseRaycastHit(hit); float meanDist = Mathf.Infinity; float meanCrashTime = Mathf.Infinity;; if (distList.Count > 0) { meanDist = distList.Average(); } if (timeList.Count > 0) { meanCrashTime = timeList.Average(); } //Debug.Log(meanCrashTime); currentSensorReport.estimatedDistanceToHit = meanDist; currentSensorReport.estimatedTimeToHit = meanCrashTime; currentSensorReport.crashValue = crashSensorValue; currentSensorReport.telecastCrashValue = telecastCrashSensorValue; currentSensorReport.playerAhead = playerAhead; currentSensorReport.leftRightCoefficient = leftRightCoefficient; }