Raycasts and vehicle traffic
I've been working on a waypoint-based traffic system but am struggling with making sure the vehicles stop adequately for stop lights and other cars. I am using modified versions of the Unity Standard Assets code for carai and control.
I started with simple box triggers, which worked for the most part but I ran into issues with ontriggerstay, ontriggerexit, and the isdriving boolean I have that tells a car to brake or move.
I then looked into raycasts, which I have never used, and am finding it difficult to use them correctly. Sometimes they work and sometimes they don't.
I'm trying to send out a raycast, which detects triggers, then decides if the trigger is a "stop" trigger, and, if it's below the braking distance, tells the car to brake. Otherwise, I want the car to move.
Here is a photo of two identical prefabs, instantiated at the very same time, traveling at two triggers with the same "stop" settings, but the one on the left is obeying the stop (slowing down) and the one on the right is not (charging ahead).
Here is my raycast code, which is called before the move function in fixedupdate:
void RaycastFront()
{
RaycastHit TheHit;
Ray ray = new Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, out TheHit))
{
TargetDistance = TheHit.distance;
}
if (TheHit.collider.isTrigger == true)
{
Debug.Log("ray hit a trigger");
if (TheHit.collider.gameObject.GetComponent<environmentcontroller>()._triggertype == environmentcontroller.triggerType.carstopbox || TheHit.collider.gameObject.GetComponent<environmentcontroller>()._triggertype == environmentcontroller.triggerType.stopbox)
{
Debug.Log("ray hit a collider: " + TheHit.collider.gameObject.GetComponent<environmentcontroller>()._triggertype);
Debug.Log("Target distance: " + TargetDistance + " brakedistance: " + brakedistance);
if (TargetDistance <= brakedistance)
{
Debug.Log("it's in range! Target distance: " + TargetDistance + " brakedistance: " + brakedistance);
m_Driving = false;
Debug.Log("car driving = " + m_Driving);
}
else { m_Driving = true; Debug.Log("car driving went back to true"); }
}
}
}
}
I understand that I don't fully understand raycasting and appreciate any guidance.
I realized through debugging that my transform.forward was going in one direction. These trucks are child objects of other gameobjects and I cannot figure out after four hours how to set the raycast to these child forwards so I tried using the destination vector3 from the waypoint system. It's still not working but I wanted to update the code for anyone able to help.
Also, now the target distance of the left vehicle says 0 and the right says 11, though they are basically the same distance from their destinations. I'm very confused
void RaycastFront()
{
RaycastHit TheHit;
Ray ray = new Ray(transform.position, destination);
if (Physics.Raycast(ray, out TheHit))
{
TargetDistance = TheHit.distance;
Debug.DrawLine(transform.position, destination, Color.white);
if (TheHit.collider.isTrigger == true)
{
Debug.Log("ray hit a trigger");
if (TheHit.collider.gameObject.GetComponent<environmentcontroller>()._triggertype == environmentcontroller.triggerType.carstopbox || TheHit.collider.gameObject.GetComponent<environmentcontroller>()._triggertype == environmentcontroller.triggerType.stopbox)
{
Debug.DrawLine(transform.position, destination, Color.yellow);
if (TargetDistance <= brakedistance)
{
Debug.DrawLine(transform.position, destination, Color.red);
//Debug.Log("it's in range! Target distance: " + TargetDistance + " brakedistance: " + brakedistance);
m_Driving = false;
//Debug.Log("car driving = " + m_Driving);
}
else { m_Driving = true; Debug.Log("car driving went back to true"); }
}
}
}
}
Your answer
Follow this Question
Related Questions
Get component from parent of children hittet with raycast 1 Answer
Foreach working very strange 0 Answers
RayCastHit 2d with layerMask not workin 0 Answers
Help with Raycast Script 0 Answers