• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CosmasStudios · Mar 14, 2017 at 09:36 AM · unity 5ontriggerentertagsontriggerexitontriggerstay

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.
alt text 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;
     }
 }

}

untitled-1.png (65.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

MonoBehaviour.OnTriggerX vs Collider.OnTriggerX 1 Answer

Problem with OnTriggerEnter 1 Answer

OnTriggerEnter only working once. 0 Answers

Problems with tags 1 Answer

Can I access a script OnTrigger WITHOUT using getcomponent? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges