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?
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.
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;
}
}
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
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?
– Chris-Dno. it can drive anywhere... there isnt a specific path. the car code is the same as the one in the car tutorial
– sketchers1Well, 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.
– syclamoth