Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Cars pass through anothers (collider)

Discussion in 'Scripting' started by Zavalichi, Oct 20, 2018.

  1. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Hi guys,
    How are you?

    Imagine you have a lot of cars in a city. Every car have a start speed and are spawned in random points on road.

    After play button was pressed I want to syncronize the cars to not pass through anothers.

    Can you help me?
    Here is my code:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class SpeedChange: MonoBehaviour
    5. {
    6.  
    7.     private void OnTriggerEnter(Collider other)
    8.     {
    9.         if (gameObject.tag == "Car")
    10.         {
    11.             if (gameObject.GetComponent<CarManager>().speed >= gameObject.GetComponent<CarManager>().stopSpeed)
    12.                 gameObject.GetComponent<CarManager>().speed -= 1f;
    13.  
    14.             Debug.Log("onTriggerEnter");
    15.         }
    16.        
    17.     }
    18.  
    19.     private void OnTriggerExit(Collider other)
    20.     {
    21.         if(gameObject.tag == "Car")
    22.         {
    23.             if (gameObject.GetComponent<CarManager>().speed <= gameObject.GetComponent<CarManager>().normalSpeed)
    24.                 gameObject.GetComponent<CarManager>().speed += 0.01f;
    25.  
    26.             Debug.Log("onTriggerExit");
    27.         }
    28.      
    29.     }
    30.    
    31.    
    32. }
    33.  
     
  2. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Maybe try use private void OnTriggerStay(Collider other) except of private void OnTriggerEnter(Collider other) because Enter is called only once when car enter to collider and you want to slow him down then its must be called in update. Or just immediately reduce his speed to going slower how car in front of him. And for exit too, its called only once when car going out of collider.
     
  3. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Can you give me an example? I don't understand :(
    The big problem is when more cars are spawned in same place because all cars are same script for collision.

    With my actual code, if some cars are spawned in some place, they got stuck
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class SpeedChange: MonoBehaviour
    5. {
    6.  
    7.     private void OnTriggerEnter(Collider other)
    8.     {
    9.         if (gameObject.tag == "Car")
    10.         {
    11.             if (gameObject.GetComponent<CarManager>().speed >= 0f)
    12.             {
    13.                 gameObject.GetComponent<CarManager>().speed -= 1f;
    14.                 if (gameObject.GetComponent<CarManager>().speed < 0f)
    15.                     gameObject.GetComponent<CarManager>().speed = 0f;
    16.             }
    17.             Debug.Log("onTriggerEnter");
    18.         }
    19.        
    20.     }
    21.  
    22.     private void OnTriggerExit(Collider other)
    23.     {
    24.         if(gameObject.tag == "Car")
    25.         {
    26.             if (gameObject.GetComponent<CarManager>().speed <= gameObject.GetComponent<CarManager>().normalSpeed)
    27.                 gameObject.GetComponent<CarManager>().speed += 0.01f;
    28.  
    29.             Debug.Log("onTriggerExit");
    30.         }
    31.      
    32.     }
    33.    
    34.    
    35. }
    36.  
    upload_2018-10-20_20-59-38.png
     
    Last edited: Oct 20, 2018
  4. SVKsuli

    SVKsuli

    Joined:
    Mar 23, 2014
    Posts:
    63
    Spawning cars into position of other is different isue that you need solve. But here is basic example how it can work:
    Code (CSharp):
    1. public class CarControl : MonoBehaviour {
    2.  
    3.     public float speed;
    4.  
    5.     public float _speed = 0;
    6.     private bool slowDown;
    7.  
    8.     void Update () {
    9.         if (slowDown == false)
    10.             _speed = speed;
    11.         else _speed -= 0.1f;
    12.  
    13.         transform.Translate(Vector3.right * Time.deltaTime * _speed);
    14.     }
    15.  
    16.     private void OnTriggerEnter(Collider other)
    17.     {
    18.         if (other.gameObject.tag == "Wall")
    19.             slowDown = true;
    20.     }
    21.     private void OnTriggerExit(Collider other)
    22.     {
    23.         if (other.gameObject.tag == "Wall")
    24.             slowDown = false;
    25.     }
    26. }
    27.  
    And example how it look like :)
    https://gyazo.com/d2b778b300da172f4b7ae4e4bf7e319c

    Yes i talking about using OnTriggerStay but this is way how to do it too.