- Home /
How would i slow a car down when it hit off road on a track
Well i had guesses that i should use something with collision but as far as how to call it into working on script i have no clue. So i was wondering if you guys could give me tips show me how to write it and the meaning of it as i am still very very new to scripting. So yeah any help is greatly appreciated thank you for your time.
Answer by Nomibuilder · Dec 02, 2014 at 02:09 PM
Here is simple code to do it. Make a duplicate Mesh Collider of Road. Check "is trigger" from inspector view. Add this Script to the road. Adjust it according to your situation.
#pragma strict
function OnTriggerExit (other : Collider)
{
if(other.gameObject.tag == "Player")
{
other.GetComponent.< CarControl>().offroad = true;
Debug.Log("Off the Road");
}
}
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.tag == "Player")
{
other.GetComponent.< CarControl>().offroad = false;
Debug.Log("back On Track");
}
}
And in your car Control Script Add this code
function offtheroad()
{
if(offroad)
{
if(Speed > offroadlimit){
Wheel_FL.brakeTorque = 300;
Wheel_FR.brakeTorque = 300;
}
else
{
Wheel_FL.brakeTorque = 0;
Wheel_FR.brakeTorque = 0;
}
}
else
{
Wheel_FL.brakeTorque = 0;
Wheel_FR.brakeTorque = 0;
}
}
Good Answer! And ultimately close to what I did. Basically, my roads were a separate mesh already. Didn't use the OnTrigger, but did a raycast down where the wheels were, and if it didn't hit Road, added a drag coefficient.
I do believe your idea is much more elegant, and I will probably change the code to match.
you'll probably want to check the collision with the non-road surface. In this situation the car will only slow down when it's completely off-road. You'll probably want it to slow down even if it's partially off-road as well.
@stevethorne Yeah right .. Raycasting is also a good approach
Answer by MFen · Dec 16, 2013 at 07:13 PM
I would use
void OnCollisionEnter(Collision other)
{
other.getcomponent<vehicleInfo>().speed /= 2;
}
void OnCollisionExit (Collision other)
{
other.getcomponent<vehicleInfo>().speed *= 2;
}
//OR
void OnTriggerEnter(Collider other)
{
}
void OnTriggerExit (Collider other)
{
}
for each of your rough areas add a cube on top of it that is the size and shape, remove the render component, now choose either trigger or collision on the box collider, when the vehicle enters you now set its speed, when it exits revert it back.
Answer by Tanoshimi2000 · Mar 31, 2014 at 01:14 AM
Here's my code, based upon the Car Tutorial.
function Check_if_Off_Road()
{
//Check to see if we're on the road or dirt
var rayLeft : Ray;
var hit : RaycastHit;
var rayStart : Transform = gameObject.Find("Collider_Bottom").transform;
rayLeft.origin = rayStart.position;
rayLeft.direction=-Vector3.up;
offRoad=false; //default
if (Physics.Raycast(rayLeft, hit))
{
//Play Sound only if moving and not on the road
if (hit.collider.gameObject.name == "Terrain") { //On ground, not on road
//Play Sound
//sound.OffRoad(true, Mathf.Clamp01(skidGroundSpeed * 0.1));
sound.OffRoad(true, 0.75);
//Reduce Speed
offRoad=true;
dragMultiplier.x = initialDragMultiplierX * handbrakeXDragFactor;
} else {
dragMultiplier.x = initialDragMultiplierX;
sound.OffRoad(false, 0);
}
}
}
Basically, it involves shooting a ray from the bottom of the center of the car. If that hits Terrain then we're off road. Would probably be better to indicate if not Road, but since my roads have custom names, it's easier to identify terrain.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
In-Game GUI buttons don't work 2 Answers
How to assign transfrom array in the code? 1 Answer
2d Object floating in space HELP! 1 Answer
(C#) Public variables that have descriptions in the engine. 2 Answers