• 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerEnter only working once. 0 Answers

How can I get my script to recognise when a collider exits while the box collider of the trigger object is disabled? 1 Answer

Problems with tags 1 Answer

How to use OnTriggerEnter with multiple triggered objects? 1 Answer

Trigger area around player. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges