- Home /
Car Getting Stuck? Reset Car
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.
Are there safe areas on your terrain that you know it wouldn't get stuck in? Is it that you have an ideal path that you intend the player to follow (that wouldn't get them stuck) but sometimes they'll stray from it?
no. it can drive anywhere... there isnt a specific path. the car code is the same as the one in the car tutorial
Well, you might want to fix up your car code so it doesn't get stuck as much! As for a reset button, how about just jumping the car back to a known 'safe' position- it's as easy as assigning transform.position to a known value.
Answer by syclamoth · Sep 18, 2011 at 04:56 AM
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.
Answer by Chuckler · Jun 09, 2012 at 06:24 PM
How about a way to reset the player/car to near or above the current position, thereby "resetting it" on a playable surface..?
Answer by ocularcash · Sep 17, 2011 at 07:14 PM
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;
}
}
Well, it is a very mountainous terrain, so there are HUNDREDS of locations where it can get stuck. I highly doubt I could track all of these
Also, the player's position will NEVER be exactly any particular point- it just doesn't happen.
Answer by ocularcash · Sep 17, 2011 at 07:14 PM
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;
}
}
Answer by sketchers1 · Sep 18, 2011 at 12:33 PM
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....
Your answer
Follow this Question
Related Questions
Spawning in Specific Areas 1 Answer
Car steering 1 Answer
Get Height of Terrain in Script 1 Answer
Start another thread for a function 1 Answer