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
0
Question by El-Deiablo · Nov 02, 2016 at 04:15 PM · move an objectsmooth followpoint-a-to-b

Smooth Movement Transition

I am trying to move the an object to the "next" point (0,6) once a certain amount of rotations has occurred. I can get the object to move to the point, but it happens instantly and not smooth like the rotation. I tried using MoveTowards, but haven't been able to make it work. What could I add?

Here's what I have currently:

 if (rotationCounter < 6) {
    
    timeCounter += Time.deltaTime * speed;
 
    float x = Mathf.Cos (timeCounter) * width;
    float y = Mathf.Sin (timeCounter) * height;
    float z = 0;
 
    transform.position = new Vector3 (x, y, z);
 
    if (buttonDown == true) {
 
     transform.position = new Vector3 (next.transform.position.x, next.transform.position.y, z);
   
    }
Comment
Add comment
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

2 Replies

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

Answer by Namey5 · Nov 03, 2016 at 06:09 AM

For smooth transitions you would ordinarily use interpolation, or lerping.

 bool move = false;
 float speed = 1;
 
 void Update ()
 {
     ...
     if (buttonDown)
         move = true;
 
     if (move)
         transform.position = Vector3.Lerp (transform.position, Vector3 (next.transform.position.x, next.transform.position.y, z), Time.deltaTime * speed);
 
     if (Vector3.Distance (transform.position, next.transform.position) < 0.02 && move)
         move = false;
     ...
 }

https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

Comment
Add comment · Show 6 · 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 El-Deiablo · Nov 03, 2016 at 09:52 PM 0
Share

Thanks for getting back to me. I checked out the unity manual and was having difficulty understanding why you put in this value for the target value - Vector3 (next.transform.position.x, next.transform.position.y, z) in line 11/12. I was unable to build the code due to have a type (Vector3) where a value should be. I changed that part to next.transform.position, but the ship doesn't teleport nor move to the target point. I may be using the code you provided in the wrong area.

Here's what I have:

void Update () {

     Rocket$$anonymous$$ove ();

     if (buttonDown == true) {

         move = true;
         StartCoroutine (CountDown ());

     }

     if (move == true) {

         transform.position = Vector3.Lerp (transform.position, next.transform.position, Time.deltaTime * speed);

     }

     if (Vector3.Distance (transform.position, next.transform.position) < 0.02 && move) {
     
         move = false;

     }

 }

 public void Rocket$$anonymous$$ove(){
         
         timeCounter += Time.deltaTime * speed;

         float x = $$anonymous$$athf.Cos (timeCounter) * width;
         float y = $$anonymous$$athf.Sin (timeCounter) * height;
         float z = 0;

         transform.position = new Vector3 (x, y, z);

 }

 public void NextPlanet(){

     level++;
     buttonDown = true;
     startCountDown = false;

 }
avatar image Namey5 El-Deiablo · Nov 04, 2016 at 04:46 AM 0
Share

I used that value ("Vector3 (next.transform.position.x, next.transform.position.y, z);") because it is the position that you supplied in the original question code snippet. Also, I forgot this was C# and you have to add the "new" keyword before the Vector3 reference. As for the code in your comment, it is completely correct, so there's no real reason why it wouldn't work unless

  1. The "speed" variable is set to 0.

  2. Your object is within 0.02 distance to the "move" transform initially.

  3. There is a problem with the "move" boolean.

The reason I have a distance check for the second is that interpolation (in this case) becomes slower the closer you get to the target, and as such you can never actually get to the exact position without making it fixed. As for the "move" boolean, you should just check that in the inspector it is being enabled.

avatar image El-Deiablo Namey5 · Nov 07, 2016 at 05:41 PM 0
Share

I tried debugging and the if(move == true) executes, but the ship shakes a little and then continues to orbit. The if(Vector3.Distance) never executes.

Here's the code:

 void Start () {
 
         countDownTimer.enabled = false;
         planetHop.enabled = false;
         planetHop.image.enabled = false;
         speed = 1f;
         height = 3f;
         width = 3f;
 
     }
     
     // Update is called once per frame
     void Update () {
 
     
     Rocket$$anonymous$$ove ();
 
         Face$$anonymous$$oveDirection();
 
         if (orbitCounter == 6) {
 
             Destroy (gameObject);
 
         }
 
         if (buttonDown == true && level==0) {
 
             move = true;
             StartCoroutine (PlanetHopCountDown ());
                         level++;
             Debug.Log (move);
 
         }
 
         if (move == true) {
 
             transform.position = Vector3.Lerp (transform.position, new Vector3 (next.transform.position.x, next.transform.position.y, 0), Time.deltaTime * speed);
             Debug.Log ("Traveling");
         }
 
         if (Vector3.Distance (transform.position, next.transform.position) < 0.02f && move == true) {
 
             Debug.Log (move);
             move = false;
 
         }
 
         if (startCountDown == false && level == 1) {
 
             countDownTimer.enabled = false;
             StopCoroutine (PlanetHopCountDown ());
 
         }
 
     }
 
 public void Rocket$$anonymous$$ove(){
 
         if (rotationCounter < 6) {
             
             timeCounter += Time.deltaTime * speed;
 
             float x = $$anonymous$$athf.Cos (timeCounter) * width;
             float y = $$anonymous$$athf.Sin (timeCounter) * height;
             float z = 0;
 
             transform.position = new Vector3 (x, y, z);
 
         }


Show more comments
avatar image
1

Answer by Vylax · Nov 09, 2016 at 09:00 AM

Try to use Mathf.SmoothDamp();

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

59 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 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 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 avatar image avatar image

Related Questions

What is the simplest way to move an object from Point A to Point B? (2D) (JS) 1 Answer

Automatic Movement Script 1 Answer

Player vehicle Movement using rigidbody 1 Answer

object moves out of the screen 0 Answers

move object with Vector3 by position 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