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 misko96 · Jan 02, 2014 at 02:28 PM · move an objectover time

Rotation and Moving object

Hi, I dont want to spam all the time with questions, so here is 3 questions: 1. I found this script for moving some object when trigger get on, but i want and that the same object move back, in starting position, if someone know how to do it please help:

     public Transform sign;
     public Vector3 signTargetOffset;
     public float raiseTime = 2;
     Vector3 signStart;
     Vector3 signTarget;
      
     public GameObject player;
      
     void Start()
     {
     signStart = sign.position;
     signTarget = signStart + signTargetOffset;
     }
      
     void OnTriggerEnter(Collider other)
     {
     if(other.gameObject == player)
     {
     StartCoroutine(RaiseSign(raiseTime));
     }
     }
      
     IEnumerator RaiseSign(float time)
     {
     float elapsedTime = 0;
     while(elapsedTime < time)
     {
     sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
     elapsedTime += Time.deltaTime;
     yield return null;
     }
     sign.position = signTarget;
     }
  1. need as well that some other object rotate in Z axis for some time, and then stop for lets say 2 sec and then again rotate, also if someone know this pls help 3.And for the last one if u can help me with making script for some object to move in Z axis and then somewhere stop and go back in starting position, and do that all the time.

Thanks everybody who answer on any of this question!

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 BigRoy · Jan 02, 2014 at 02:58 PM 0
Share

Unless the questions are very relevant to each other feel free to create separate questions im the future. It might seem that you're asking a lot of questions at the same time, but in the long run people will be able to find back and google the question-answer relationships much easier if everything is focused on a single simple thing. Currently I'll see if I can answer this one for the biggest part here. :)

1 Reply

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

Answer by BigRoy · Jan 02, 2014 at 03:24 PM

1. Move to position on OnTriggerEnter() and move back on OnTriggerExit()

The following should start the raiseSign coroutine whenever the player enters the trigger. It force stops that coroutine whenever it leaves it again and then starts the lowerSign coroutine (and vice versa).

 public Transform sign;
 public Vector3 signTargetOffset;
 public float raiseTime = 2;
 public GameObject player;
 
 private Vector3 signStart;
 private Vector3 signTarget;    
 private bool isEnabled = false;
 private float elapsedTime = 0;

 void Start()
 {
     signStart = sign.position;
     signTarget = signStart + signTargetOffset;
 }
 
 void OnTriggerEnter(Collider other)
 {
     if(!isEnabled && other.gameObject == player)
     {
         isEnabled = true;
         StopCoroutine("LowerSign");
         StartCoroutine("RaiseSign", raiseTime);
     }
 }
 
 void OnTriggerExit(Collider other)
 {
     if(isEnabled && other.gameObject == player)
     {
         isEnabled = false;
         StopCoroutine("RaiseSign");
         StartCoroutine("LowerSign", raiseTime);
     }
 }
 
 IEnumerator RaiseSign(float time)
 {
     // Move to the raised position
     while(elapsedTime < time)
     {
         sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
         elapsedTime += Time.deltaTime;
         yield return null;
     }
     
     elapsedTime = time;
     sign.position = signTarget;
 }
 
 IEnumerator LowerSign(float time)
 {
     // Come back from the raised position
     while(elapsedTime > 0)
     {
         sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
         elapsedTime -= Time.deltaTime;
         yield return null;
     }

     elapsedTime = 0;
     sign.position = signStart;
 }

Old (Before edit:) I had this here before. I misunderstood the question, but I'm keeping it here as a reference on how to do the 'back and forth' motion at once.

     public Transform sign;
     public Vector3 signTargetOffset;
     public float raiseTime = 2;
     Vector3 signStart;
     Vector3 signTarget;
     
     public GameObject player;
     
     void Start()
     {
         signStart = sign.position;
         signTarget = signStart + signTargetOffset;
     }
     
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject == player)
         {
             StartCoroutine(RaiseSign(raiseTime));
         }
     }
     
     IEnumerator RaiseSign(float time)
     {
             // Move to the raised position
         float elapsedTime = 0;
         while(elapsedTime < time)
         {
             sign.position = Vector3.Lerp(signStart, signTarget, elapsedTime / time);
             elapsedTime += Time.deltaTime;
             yield return null;
         }
         sign.position = signTarget;

             // Move back from the raised position
         elapsedTime = 0;
         while(elapsedTime < time)
         {
             sign.position = Vector3.Lerp(signTarget, signStart, elapsedTime / time);
             elapsedTime += Time.deltaTime;
             yield return null;
         }
         sign.position = signStart;
     }

Note about your collisions and the Coroutine running multiple routines at the same time.

Note that since you're using a Coroutine for this that it is possible that the player runs into the collision area, runs out in the meantime and back in while it's still performing the routine. This will result in having two of the same Coroutines going on at being performed at the same time.

A possible solution is to have a variable that stores whether it's already raising the sign, if so it will not trigger it again until it ended. (Shouldn't be too hard to add. Let me know if you want me to add an example of this quickly.)

2. Move more objects

This should be a slight modification on the script above. You can add extra public Transform sign variables and then also include those in the coroutine, or you could add extra coroutines for them.

To apply rotational changes instead of doing sign.position use sign.rotation. You can do this because the sign variable in the script is actually a Transform object.

3. Keep moving an object ever since the first collision.

This sounds like it's activating another script. Instead of writing out the script for you I'll give you some pointers, here are some possible ways to proceed:

  • Enable a component on the object you want to keep moving from within this script

  • Set a public variable on another object to True and perform your movements in that script.

  • You could also (I think) initialize a looping animation from that point using Unity's animation system.

Comment
Add comment · Show 4 · 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 misko96 · Jan 02, 2014 at 03:44 PM 0
Share

I dont think u understood me for this 1st question ( or more like i didnt explain good) I dont want to it go back right when it come to its destination, I want to it stay there while Player dont Exit collision area, I will look other 2 questions later, I dont have time now, but thanks anyway if u dont know how to do this what i want

avatar image BigRoy · Jan 02, 2014 at 05:59 PM 0
Share

@misko96 I did misunderstand the question, sorry about that. I edited my answer based on your comment. At the top you should find code that should do exactly what you described.

avatar image misko96 · Jan 02, 2014 at 07:11 PM 0
Share

For this 2nd question again my english is sooo bad so problably i didnt explain it good either, it doesnt need to be connected to any collider or whatever, i just want that it rotate independently on anyhing, just that it rotate for lets say 3 sec, then stops, and then again rotate, and same for 3rd question, just that it move, and come back and again and again( even i didnt understand your answer on that question either, so maybe u wanted to say that)

avatar image misko96 · Jan 02, 2014 at 07:31 PM 0
Share

And forgot to say this script is great, thank you

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Rotate overtime to match waypoint 1 Answer

Change speed over time 1 Answer

Rotating an Object while Moving 0 Answers

Endless runner, road segments spawn speed 0 Answers


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