- Home /
OntriggerEnter / Stay with the same Gameobject tags
Hey all. I have 2 AI cars with each having the same tag and 2 box colliders with is triggered checked to check when to brake. The problem is when the car behind reaches the front car (Or in other words when the box collider of the behind car collides with the front car), They both Brake knowing that the front car did not collide with anything as there is not a car in front of it. What I want is the front Car not to use the brake function or I want it to stay moving when the back car's box collider detects the front car.
Here's the code:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class CarAI : MonoBehaviour {
public Transform pathGroup;
public Vector3 centerOfMass;
public float maxSteer = 15;
public float maxTorque = 50;
public float currentSpeed;
public float MaxBrakeTorque;
public float minCarSpeed;
public float topSpeed = 150;
public float decelerationSpeed = 10;
public bool brake = false;
public WheelCollider wheelFL;
public WheelCollider wheelFR;
public WheelCollider wheelRL;
public WheelCollider wheelRR;
Rigidbody RG;
public int currentPathObj;
public float distFromPath;
private List<Transform> path;
private Rigidbody rb;
void Start()
{
RG = GetComponent<Rigidbody> ();
path = new List<Transform>();
rb = GetComponent<Rigidbody>();
rb.centerOfMass = centerOfMass;
getPath();
}
void OnTriggerStay(Collider target)
{
if (target.tag == "Car") {
this.brake = true;
}
}
void OnTriggerExit(Collider target)
{
if(target.tag == "Car")
{
this.brake = false;
}
}
void Update()
{
getSteer();
if (brake == false) {
RG.drag = 0.1f;
Move ();
wheelRL.brakeTorque = 0;
wheelRR.brakeTorque = 0;
wheelFR.brakeTorque = 0;
wheelFL.brakeTorque = 0;
}
else if (brake == true) {
RG.drag = 2f;
Brake ();
}
}
void getPath()
{
Transform[] childObejects = pathGroup.GetComponentsInChildren<Transform>();
for (int i = 0; i < childObejects.Length; i++)
{
Transform temp = childObejects[i];
if (temp.gameObject.GetInstanceID() != GetInstanceID())
path.Add(temp);
}
Debug.Log(path.Count);
}
void getSteer()
{
Vector3 steerVector = transform.InverseTransformPoint(new Vector3(path[currentPathObj].position.x, transform.position.y, path[currentPathObj].position.z));
float newSteer = maxSteer * (steerVector.x / steerVector.magnitude);
wheelFL.steerAngle = newSteer;
wheelFR.steerAngle = newSteer;
if (steerVector.magnitude <= distFromPath)
{
currentPathObj++;
}
if (currentPathObj >= path.Count)
{
currentPathObj = 0;
}
}
void Brake(){
if (currentSpeed >= minCarSpeed) {
wheelRL.brakeTorque = MaxBrakeTorque;
wheelRR.brakeTorque = MaxBrakeTorque;
wheelFR.brakeTorque = MaxBrakeTorque;
wheelFL.brakeTorque = MaxBrakeTorque;
}
}
void Move()
{
currentSpeed = 2 * (22 / 7) * wheelRL.radius * wheelRL.rpm * 60 / 1000;
currentSpeed = Mathf.Round(currentSpeed);
if (currentSpeed <= topSpeed)
{
wheelRL.motorTorque = maxTorque;
wheelRR.motorTorque = maxTorque;
wheelRL.brakeTorque = 0;
wheelRR.brakeTorque = 0;
}
else
{
wheelRL.motorTorque = 0;
wheelRR.motorTorque = 0;
wheelRL.brakeTorque = decelerationSpeed;
wheelRR.brakeTorque = decelerationSpeed;
}
}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter only working once. 0 Answers
How to use OnTriggerEnter with multiple triggered objects? 1 Answer
Problems with tags 1 Answer
Trigger area around player. 1 Answer