Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Crypt0nimous · Jun 16, 2015 at 08:56 PM · respawngradually

How to gradually move to a specific location?

Hey guys, I'm working on a game where you go back to the beginning of the level if you touch a wall. The game is working perfectly, and to respawn i use this C# script:

 public void RespawnSphere () {
         StartCoroutine(SmoothRespawn());
     }
 
     IEnumerator SmoothRespawn () {
 
         isRunning = false;
         yield return new WaitForSeconds(1f);            
         transform.position = spawnPoint.position;
         transform.rotation = spawnPoint.rotation;                
         yield return new WaitForSeconds (1f);
         isRunning = true;
 
     }

With this, on contact with the wall, the player stops moving for a second, immediately reappears at the spawn point, and then starts moving again after a second. While this works well, the change of location is too sudden and i would like to make the player gradually move to the spawn point (over the course of a second). I tried using iTween's MoveTo method, but it didn't work. This is the code I used:

 public void RespawnSphere () {
     StartCoroutine(SmoothRespawn());
 }

 IEnumerator SmoothRespawn () {

     isRunning = false;
     yield return new WaitForSeconds(0.75f);            
     iTween.MoveTo (gameObject, new Vector3 (-95f, 1f, 52.5f), 1f);        
     transform.rotation = spawnPoint.rotation;                
     yield return new WaitForSeconds (1f);
     isRunning = true;

 }

The player stops when touching a wall, but doesn't go back to the spawn point. I think it's something to do with IEnumerator, but i simply can't figure it out. I would greatly appreciate it if you could help me. Thanks.

Comment
Add comment · Show 1
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 Crypt0nimous · Jun 17, 2015 at 03:31 PM 0
Share

Never$$anonymous$$d, i figured it out. Thank you so much!

3 Replies

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

Answer by bogartrye · Jun 17, 2015 at 04:33 AM

When you call WaitForSeconds, the function is stopped for that amount of time. When you do transform.position = spawnPoint.position after the wait, it will set the position of the player to the spawn point instantly. To get a gradual change, you need to change the position gradually. Here is what you want:

  IEnumerator SmoothRespawn () {
             
             isRunning = false;
             // Getting the difference in position and rotation
             Vector3 deltaPos = spawnPoint.position - transform.position;
             Vector3 deltaRotation = spawnPoint.rotation.eulerAngles - transform.rotation.eulerAngles;
             // Repeat once per frame for a second:
             float timePassed = 0;
             while (timePassed < 1.0f) {
                 // Increment the time to prevent an infinite loop
                 timePassed += Time.deltaTime; 
                 // Slowly adjust the position and rotation so it approaches the spawn position and rotation
                 transform.position += deltaPos * Time.deltaTime;
                 transform.Rotate(deltaRotation * Time.deltaTime);
                 // Exit the function (it will begin where it left off the next frame)
                 yield return null;
             }
             // Ensures the player is exactly where you want him when the time is up           
             transform.position = spawnPoint.position;
             transform.rotation = spawnPoint.rotation; 
             // Waits for a second after the player has returned before running the game again
             yield return new WaitForSeconds (1f);
             isRunning = true;
     
         }


Note that if you to change the respawn time from 1 second, you will need to change the speed that you translate and rotate the player. Otherwise, the player will either move past the spawn point and then jump to the correct location or only make it half way before jumping there. To do this, just divide the amount of movement by the respawn time (i.e., transform.position += deltaPos * Time.deltaTime / respawnTime).

Also, an important thing to remember is to include the yield return null inside your while loop. Without it, the loop will complete in one frame and the effect will be the same as you had before. Calling yield will allow Unity to update before the position reaches the destination, resulting in the desired animation.

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 Crypt0nimous · Jun 17, 2015 at 02:27 PM 0
Share

thanks so much for the answer, but the player can respawn through walls, will this be a problem? it might retrigger the whole thing making it jumpy

avatar image Crypt0nimous · Jun 17, 2015 at 03:32 PM 0
Share

Never$$anonymous$$d, i figured it out. Thank you so much!

avatar image
1

Answer by Meltdown · Jun 17, 2015 at 04:41 AM

Rather handle these sorts of tasks in your Update() method than using a co-routine.

Use Vector3.MoveTowards

     public Transform target;
     public float speed;
     void Update() {
         float step = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
     }


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 Calum1015 · Jun 16, 2015 at 11:01 PM

I would recommend using Transform.Translate to move the player. This way you can set the speed and adjust it however you like. You can find that page in the scripting reference here. I'll type you a code snippet to get you started though :) public float speed; public Transform destination; public void RespawnSphere () { StartCoroutine(SmoothRespawn()); } IEnumerator SmoothRespawn () { isRunning = false; yield return new WaitForSeconds(1f); transform.Translate(Vector3.backwards * speed * Time.deltaTime, destination); transform.rotation = spawnPoint.rotation; yield return new WaitForSeconds (1f); isRunning = true; } Hope this helps!

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

How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers

Respawn player at different Spawnpoints 1 Answer

How to create a fade out respawn from falling? 1 Answer

Character losing life event not firing 1 Answer

I want to respawn player to prefab. this is what i've got at the moment. What am I doing wrong? 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