Hi, sorry for the descriptive title.
So I am making realistic physics based traffic. I want my cars to brake with raycasts only when the speed of the car infront is in a certain range. I’ll show a code snippet of my raycast function.
private void Raycasting()
{
//Origin off the raycast on the body of the car.
ForwardVector = new Vector3(transform.position.x + (transform.forward.x * bc.size.z / 2 + 0.5f), transform.position.y + 0.5f, transform.position.z + (transform.forward.z * bc.size.z / 2 + 0.1f));
LeftRightVector = new Vector3(transform.position.x + (transform.forward.x * bc.size.z / 2 + 0.5f), transform.position.y + 0.5f, transform.position.z + (transform.forward.z * bc.size.z / 2 + 0.1f));
float RayLength = (Kmph / AdjustLengthRay) + Offset;
AdjustBrakeTorque = 2000f;
Offset = 3f;
AdjustLengthRay = 1.2f;
//Ray(s)
Ray FRay = new Ray(ForwardVector, transform.forward * RayLength);
//Ray LRay = new Ray(LeftRightVector - transform.right, transform.forward * RayLength);
//Ray RRay = new Ray(LeftRightVector + transform.right, transform.forward * RayLength);
//Draw ray(s) to debug
Debug.DrawRay(ForwardVector, transform.forward * RayLength);
//Debug.DrawRay(LeftRightVector - transform.right, transform.forward * RayLength);
//Debug.DrawRay(LeftRightVector + transform.right, transform.forward * RayLength);
//if ((Physics.Raycast(FRay, out hit, RayLength) || Physics.Raycast(LRay, out hit, RayLength) || Physics.Raycast(RRay, out hit, RayLength)) && hit.transform.CompareTag("Car"))
if (Physics.Raycast(FRay, out hit, RayLength) && hit.transform.CompareTag("Car"))
{
// Maybe add the functionality that cars will look on-hit what the speed is of the car that was hit with the raycast and if the speed difference is x amount bigger then brake?
KmphOther = transform.InverseTransformVector(hit.rigidbody.velocity).z * 3.6f;
Debug.Log(Mathf.Abs(KmphOther - Kmph));
if (Mathf.Abs(KmphOther - Kmph) > 7.5f)
{
torque = 0f;
DistanceToObject = hit.distance;
BrakeTorque = transform.InverseTransformVector(rigbody.velocity).z / DistanceToObject * AdjustBrakeTorque;
Debug.DrawRay(ForwardVector, transform.forward * DistanceToObject, Color.red);
//Debug.DrawRay(LeftRightVector - transform.right, transform.forward * DistanceToObject, Color.red);
//Debug.DrawRay(LeftRightVector + transform.right, transform.forward * DistanceToObject, Color.red);
}
else
{
BrakeTorque = 0f;
}
}
else
{
BrakeTorque = 0f;
}
}
So what I am doing here is, if the speed difference is greater then 7.5 kmph then start braking. this works but when the cars are almost at a standstill the difference in speed will be smaller then 7.5 again and the cars will accelerate again and this is not what I want. I need some extra code that will keep the cars braking untill the object/car infront of them starts moving again. if a lot of the code needs to change I don’t mind. I just would like to know a solution to my issue that I’m having.
Also the 2x BrakeTorque = 0f; is maybe not necessary but I just put it in just in case.