So, I have a car game, where the car has to drive over… “Heavy Terrain”, and a lot of times the car gets stuck in small crevices etc. I wanted to figure out how to make the car “reset” with button… So what happens is the car gets out of the crevice, but does not restart the level. (I’ve only been doing Javascript for about a month so I might need a little explaining)

I have a video to show the car getting stuck, but im not sure how to post it.ThAT would probably describe more about what I am talking about.

Ok, to expand on my comment earlier- You would want to set up a ‘reset’ button that the player presses to get them out of a mess. If you know some places on your map that are ‘safe’, you can put transform empties in all of them, and then collect them into an array in a script, like so-

public Transform[] safePoints;

Then in the editor drag and drop them all onto your script.

Then in your update method, do something like this:

void Update()
{
    // You have to set up an input button in the Input manager for this!
    if(Input.GetButtonDown("reset"))
    {
        // put this in a different function for general cleanliness
        ResetCar();
    }
}

void ResetCar()
{
    // first, find the closest safe place
    Transform closestTransform;
    float closestDistance = 9999999999;
    Vector3 currentPos = transform.position;
    // This goes through every possible safe place and picks the best one
    foreach(Transform trans in safePoints)
    {
        float currentDistance = Vector3.Distance(currentPos, trans.position);
        if(currentDistance < closestDistance)
        {
            closestDistance = currentDistance;
            closestTransform = trans;
        }
    }

    // Now we reset the car!
    transform.position = closestTransform.position;
    transform.rotation = closestTransform.rotation;
}

Remember you need to set a thing up in your input manager, but you should do that with all your buttons anyway.

a somewhat easy fix for that is to locate all the spots that you’ll get stuck at and respawn the car back to a safe location. The script will look similar to this

function Update()
{

if(transform.position == Vector3(1048, 13, 1380))

{

transform.position.y = 13;

transform.position.x = 1042;

transform.position.z = 1370;

}

}

try finding the locations that you’re getting stuck at and adjust this code to your positions

function Update(){

if(transform.position == Vector3(1048, 13, 1380))

{

transform.position.y = 13;

transform.position.x = 1042;

transform.position.z = 1370;

}

}

is there a way to do it like this kinda?:

function OnGUI () {
// Make a background box
GUI.Box (Rect (10,10,100,180), "Menu");


if (GUI.Button (Rect (20,40,80,20), "Levels")) {
	Application.LoadLevel ("MainMenue");

}
if (GUI.Button (Rect (20,80,80,20), “Reset”)) {
ResetCar();
}
}

void ResetCar()
{
// first, find the closest safe place
Transform closestTransform;

float closestDistance = 9999999999;

Vector3 currentPos = transform.position;

// This goes through every possible safe place and picks the best one

foreach(Transform trans in safePoints)

{

    float currentDistance = Vector3.Distance(currentPos, trans.position);

    if(currentDistance < closestDistance)

    {

        closestDistance = currentDistance;

        closestTransform = trans;

    }

}

// Now we reset the car!

transform.position = closestTransform.position;

transform.rotation = closestTransform.rotation;

}

Also, I dont get the last 2 lines on how to reset the car… Do I make separate objects called safePOints??? or…

How about a way to reset the player/car to near or above the current position, thereby “resetting it” on a playable surface…?