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

Car lights problem

Discussion in 'Scripting' started by cruising, Jan 11, 2016.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!


    Need some help with the lights for the cars.
    The car use brakelights while no speed, it use brakelights while you brake, and turns them off if you release the brake button S or Downarrow

    Now my problem is to get if the rigibody (car) goes backwards to light the reverse lights and turn them of when you stop the car.

    Any tips?

    CODE:
    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         //Brake
    4.         if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) || GetComponent<Rigidbody>().velocity.sqrMagnitude < .01)
    5.         {
    6.             Brakelights.material = brakeligtON;
    7.             RevLights.material = revLightsOFF;
    8.         }
    9.         //No Brake
    10.         else
    11.         {
    12.             Brakelights.material = brakeligtOFF;
    13.         }
    14.         //Reverse (my problem)
    15.         if(Input.GetKey(KeyCode.DownArrow)|| Input.GetKey(KeyCode.S))
    16.         {
    17.             if(Reversing the car))
    18.             {
    19.                 RevLights.material = revLightsON;
    20.             }
    21.         }
     
  2. specterdragon

    specterdragon

    Joined:
    Dec 30, 2013
    Posts:
    21
    I'm curious - You're code looks like it should work fine (haven't actually tested it though). So what is it you're having an issue with? Is it throwing an error? Are you trying to figure out how to determine if "(Reversing the car)"? Or is it something else you're trying to figure out?
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Thanks for your reply!

    Yes everything works perfect for the moment. Thats correct, im trying to determine if the car is reversing or not, and if it does i want the reverse lights to light up and the brake light to stop light.

    Do you get what i mean? i often explain like a bag of peanuts.
     
  4. specterdragon

    specterdragon

    Joined:
    Dec 30, 2013
    Posts:
    21
    LOL - No worries!

    What object is this script on: the car, the light, or something else?

    When your moving the car, how do you determine that it should be in reverse? In other words, are you using a boolean flag that says "go backward," are you using a velocity that goes negative, or something else? I would think that you can use the same check here.

    For example, if the key commands control the car's velocity then you could simply check if the velocity is < 0.
     
  5. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    It's weird your code :) There will be better this way? I hope it helps you:)
    Code (CSharp):
    1. public GameObject Brakelights;
    2.     public GameObject RevLights;
    3.     private bool isRevercing = false;
    4.     // Use this for initialization
    5.     void Start () {
    6.    
    7.     }
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.  
    12.         //Movement forward
    13.         if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.W))
    14.         {          
    15.             RevLights.gameObject.SetActive(false);
    16.         }
    17.         // Brake key
    18.         if(Input.GetKey(KeyCode.Space))
    19.         {
    20.  
    21.             Brakelights.gameObject.SetActive(true);
    22.             RevLights.gameObject.SetActive(false);
    23.             isRevercing = false;
    24.         }
    25.         else
    26.         {
    27.             Brakelights.gameObject.SetActive(false);
    28.         }
    29.  
    30.         //Reversing keys
    31.         if (Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S))
    32.         {
    33.             isRevercing = true;
    34.             if(isRevercing == true)
    35.             {
    36.                 Brakelights.gameObject.SetActive(false);
    37.                 RevLights.gameObject.SetActive(true);
    38.             }
    39.         }
    40.     }
     
    Last edited: Jan 14, 2016
  6. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    The script are on each car i have.
    I actually dont know that, im using Edys vehicle physics and i have not looking throught the script for references, i might do that to see if i cand find something.

    How can this be a better way? how can i set a gameobject to active and make it glow/not glow with only 1 gameobject? if a gameobject aint active its invisible? and then i need 2 setups of each gameobject. I dont know if i got you wrong here?:)
    And "space" is a handbrake, so it should not light up the brakelights, only with the brake pedal.
    So i need to check if the rigidbody is moving backwards to then light up the reverse lights and stop the brake lights to glow :)
    I think changing materials is a good and simple way to use on only 1 gameobject? i might be wrong.
     
  7. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    I did what is indicated in the title of the forum (Car Lights).
     

    Attached Files:

    Last edited: Jan 14, 2016
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  9. LoopSRL

    LoopSRL

    Joined:
    Mar 1, 2013
    Posts:
    3
    Hi cruising. The simplest way to get a vehicle velocity is to use this line of code
    Code (JavaScript):
    1. var speed : float;
    2. speed = transform.InverseTransformDirection(rigidbody.velocity).z;
    3. // this will return the actual speed in m/s
    4. // if u need kph simply multiply the speed per 3.6
    5. var kph : float;
    6. kph = speed * 3.6;
    Then u have to check if speed is below 0.0, that means you're going backward and turn on/off your lights.
    And make sure to check for Inputs change. I'll explain:
    If your car is not moving, then it's speed is 0. As soon as u press the W or UpArrow keys the car starts moving forward. That means that W /UpArrow are the keys for applying throttle, meanwhile S/DownArrow must be used for braking.
    At this point things are pretty simple. But what happens if we're going in reverse? Guess what...you need to flip those Inputs so S/DownArrow is negative throttle and W/UpArrow is brake.
    I've done this so many times using WheelColliders that my brain is still spinning :). If u need help with the code just ask for it
     
  10. LoopSRL

    LoopSRL

    Joined:
    Mar 1, 2013
    Posts:
    3
    It's me again. I was thinking about the code so...this is what I came up with:
    Code (CSharp):
    1. public GameObject brakeLight; // Your brake light gameObject;
    2. public GameObject reverseLight; // Your reverse light gameObject
    3. private bool reverse = false; // boolean to know if we want to go in reverse or not
    4. private float speedKPH = 0.0f; // car speed in KPH
    5.  
    6. private float motor = 0.0f; // don't touch this
    7. private float brake = 0.0f; // this one either
    8. private float forwardInput = 0.0f; // Your forward input
    9. private float backwardInput = 0.0f; // Your backward input
    10.  
    11. void Update () {
    12.     speedKPH = transform.InverseTransformDirection(rigidbody.velocity).z * 3.6;
    13.  
    14.     forwardInput = Mathf.Clamp(Input.GetAxis("Vertical"), 0.0f, 1.0f);
    15.     backwardInput = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1.0f, 0.0f);
    16.  
    17.     if(Mathf.Abs(speedKPH) < 0.1) {
    18.         if(forwardInput)
    19.             reverse = false;
    20.         else if(backwardInput)
    21.             reverse = true;
    22.     }
    23.  
    24.     if(reverse) {
    25.         motor = -backwardInput;
    26.         brake = forwardInput;
    27.  
    28.         reverseLight.GetComponent<Renderer>().material = your_reverse_light_on_material;
    29.     } else {
    30.         motor = forwardInput;
    31.         brake = backwardInput;
    32.  
    33.         reverseLight.GetComponent<Renderer>().material = your_reverse_light_off_material;
    34.     }
    35.  
    36.     if(brake > 0.0) {
    37.         brakeLight.GetComponent<Renderer>().material = your_brake_light_on_material;
    38.     } else {
    39.         brakeLight.GetComponent<Renderer>().material = your_brake_light_off_material;
    40.     }
    41.  
    42.     //Do whatever you want with it like apply WheelColliders motorTorque and brakeTorque
    43.     // REMEMBER: wheelColliders 'motorTorque' must be controlled by 'MOTOR' not by forwardInput
    44.     // and 'brakeTorque' must be controlled by 'BRAKE' not by backwardInput
    45. }
     
  11. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello.

    ill try to explain as good as i can :)
    I already have vehicle physics with engine etc.
    I have added a separate script for lights only (not lights that lights up objects, just visuals).

    I use materials with emission to simulate glowing lights like brakes and "idle" lights, i just have the reverse lights left to get to work, so i dont know if i need it that complex as in your script?

    ill post the script so you understand how im doing this.

    CODE:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BrakeingLights : MonoBehaviour {
    5.  
    6.     public Renderer Brakelights;
    7.     public Material brakeligtON;
    8.     public Material brakeligtOFF;
    9.  
    10.     public Renderer Headlights;
    11.     public Material headlightsON;
    12.     public Material headlightsOFF;
    13.  
    14.     public Renderer RevLights;
    15.     public Material revLightsON;
    16.     public Material revLightsOFF;
    17.  
    18.     public Renderer turnsignalLEFT;
    19.     public Renderer turnsignalRIGHT;
    20.     public Material turnsignalON;
    21.     public Material turnsignalOFF;
    22.  
    23.     public Light headLightRight;
    24.     public Light headLightLeft;
    25.  
    26.     private bool RightSignalON = false;
    27.     private bool LeftSignalON = false;
    28.  
    29.     void Start () {
    30.    
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update ()
    35.     {
    36.         //Brake
    37.         if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) || GetComponent<Rigidbody>().velocity.sqrMagnitude < .01)
    38.         {
    39.             Brakelights.material = brakeligtON;
    40.         }
    41.         //No Brake
    42.         else
    43.         {
    44.             Brakelights.material = brakeligtOFF;
    45.         }
    46.  
    i use this line of code to check if the rigdibody aint moving to have the brakelights to light up when the car is in idle, isnt there a similar simple line of code that i can use?

    Code (CSharp):
    1. etComponent<Rigidbody>().velocity.sqrMagnitude < .01)
     
  12. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you can't use the magnitude to tell if you are going in reverse since it's a sqrt/sq so it's lost it's sign. You are using the same key for "backwards" and "brake" so you can't use the input alone either. Look at the dot function as I mentioned above. It'll tell you if you are moving in the same direction as your facing (i.e. forwards) or in the opposite direction (i.e. backwards). It's a simple function call and comparison.
     
  13. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I don't know if you drive but the reverse lights on my car actually light up as soon as I put the car into the reverse gear. Maybe Edy's framework provides a way for you to ask which gear you are in.
     
  14. roojerry

    roojerry

    Joined:
    Mar 18, 2013
    Posts:
    68
    ^this, ideally

    There must be some way to retrieve relevant information from the vehicle controller. No reason that the brakes lights need to handle input separately if the vehicle already does. This could become a potential problem down the line if you ever decide to modify the controls of your vehicle. Now you would have to remember to update the input handling in multiple places.

    Leave the input handling to the vehicle controller and let the lights respond to the state of the vehicle.
     
  15. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    I was trying that dot function, but i could not figure out how to use it properly

    Like cars always have done :) yes it tells you, from -1 (rev) to how many gears you set up the car to have,idk if i dare t touch that complex car controller Edys have lol

    Ill take a look in the controller and see if im up to do this via the state of the vehicle
     
  16. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    You don't need to "touch" it. Just "look" at it.

    You can safely read the public properties this controller exposes. If one of those properties is "Gear" and if it has a value of -1 when you are in reverse, then I say use that condition to determine whether or not to show the reverse lights.
     
  17. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    I took a look and fixed reverse lights and brake lights while the car is in idle, but i could not find anything about the actually brakes in the "VehicleStandardInput" when you are driving and want to slow down.