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.
2 Answers
2I 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.
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.