Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by sketchers1 · Aug 19, 2011 at 03:14 PM · terraincarresetgui-button

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris D · Sep 17, 2011 at 02:04 AM 0
Share

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?

avatar image sketchers1 · Sep 17, 2011 at 02:12 AM 0
Share

no. it can drive anywhere... there isnt a specific path. the car code is the same as the one in the car tutorial

avatar image syclamoth · Sep 17, 2011 at 02:16 AM 0
Share

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.

5 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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..?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;

}

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sketchers1 · Sep 17, 2011 at 11:36 PM 0
Share

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

avatar image syclamoth · Sep 18, 2011 at 04:48 AM 0
Share

Also, the player's position will NEVER be exactly any particular point- it just doesn't happen.

avatar image
0

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;

}

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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....

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

car script / powering wheel colliders 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges