Hi On my car racing game i have a terrain and a model of a track. I want things such as the steering and speed to change when on the ‘rough’ terrain. How would i go about coding that? I know i would have to trigger a change in the code but thats as far as my knowledge goes.
thanks.
anyone? please
I’d create an invisible trigger (just a collider with “isTrigger” checked). Make it a prefab. Place the prefabs on the rough patches, and stretch it to be the right size.
Then use function onTriggerEnter in your car to
- check the kind of trigger you’re in, probably by its tag
- modify the driving conditions accordingly
Use function onTriggerExit in your car to
- check the kind of trigger you’re in
- set the driving conditions back to “normal” if you’re leaving the “rough road” trigger
Thankyou for your help. I am not very good at coding but willing to learn. I am wanting to first of all change the top speed of the car when it is on the terrain instead of my track model. I am unsure how to reference the code for the car speed in my new code. Here is my attempt
var maxSpeed : TopSpeed;
function OnTriggerEnter(Terrain:Collider){
car.script.topspeed = maxSpeed;
}
function OnTriggerExit(Terrain:Collider){
car.script.topspeed = maxSpeed;
}
The name of the script which contains the speed variable is ‘Car’ and the name of the variable is ‘Top Speed’.
Could you help me further please? Thankyou again
The script you wrote goes in the car script! When it bumps into the triggers on the track, do something like this:
public var fl_high_speed : float;
public var fl_low_speed : float;
public var fl_current_max_speed : float;
function OnTriggerEnter(go_bumped_into:Collider){
fl_current_max_speed = fl_low_speed;
}
function OnTriggerExit(go_bumped_into:Collider){
fl_current_max_speed = fl_high_speed;
}
(I changed the variable names so they made more sense to me.) Now if there are other triggers in your game, you’ll need to:
- put a named tag on each trigger
- check go_bumped_into.tag during OnTriggerEnter and OnTriggerExit so that you know what action to take based on what you bumped into.