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.
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?
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.
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.
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.
Hi cruising. The simplest way to get a vehicle velocity is to use this line of code
var speed : float;
speed = transform.InverseTransformDirection(rigidbody.velocity).z;
// this will return the actual speed in m/s
// if u need kph simply multiply the speed per 3.6
var kph : float;
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
It’s me again. I was thinking about the code so…this is what I came up with:
public GameObject brakeLight; // Your brake light gameObject;
public GameObject reverseLight; // Your reverse light gameObject
private bool reverse = false; // boolean to know if we want to go in reverse or not
private float speedKPH = 0.0f; // car speed in KPH
private float motor = 0.0f; // don't touch this
private float brake = 0.0f; // this one either
private float forwardInput = 0.0f; // Your forward input
private float backwardInput = 0.0f; // Your backward input
void Update () {
speedKPH = transform.InverseTransformDirection(rigidbody.velocity).z * 3.6;
forwardInput = Mathf.Clamp(Input.GetAxis("Vertical"), 0.0f, 1.0f);
backwardInput = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1.0f, 0.0f);
if(Mathf.Abs(speedKPH) < 0.1) {
if(forwardInput)
reverse = false;
else if(backwardInput)
reverse = true;
}
if(reverse) {
motor = -backwardInput;
brake = forwardInput;
reverseLight.GetComponent<Renderer>().material = your_reverse_light_on_material;
} else {
motor = forwardInput;
brake = backwardInput;
reverseLight.GetComponent<Renderer>().material = your_reverse_light_off_material;
}
if(brake > 0.0) {
brakeLight.GetComponent<Renderer>().material = your_brake_light_on_material;
} else {
brakeLight.GetComponent<Renderer>().material = your_brake_light_off_material;
}
//Do whatever you want with it like apply WheelColliders motorTorque and brakeTorque
// REMEMBER: wheelColliders 'motorTorque' must be controlled by 'MOTOR' not by forwardInput
// and 'brakeTorque' must be controlled by 'BRAKE' not by backwardInput
}
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:
using UnityEngine;
using System.Collections;
public class BrakeingLights : MonoBehaviour {
public Renderer Brakelights;
public Material brakeligtON;
public Material brakeligtOFF;
public Renderer Headlights;
public Material headlightsON;
public Material headlightsOFF;
public Renderer RevLights;
public Material revLightsON;
public Material revLightsOFF;
public Renderer turnsignalLEFT;
public Renderer turnsignalRIGHT;
public Material turnsignalON;
public Material turnsignalOFF;
public Light headLightRight;
public Light headLightLeft;
private bool RightSignalON = false;
private bool LeftSignalON = false;
void Start () {
}
// Update is called once per frame
void Update ()
{
//Brake
if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) || GetComponent<Rigidbody>().velocity.sqrMagnitude < .01)
{
Brakelights.material = brakeligtON;
}
//No Brake
else
{
Brakelights.material = brakeligtOFF;
}
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?
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.
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.
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.
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
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.
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.