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
1
Question by pantaryl · Oct 03, 2012 at 01:25 AM · vector3quaternionlerpmovetowardsrotatetowards

Vector3.MoveTowards and Quaternion.RotateTowards

Hello all –

I've got an interesting situation. I'm making a game where the player is able to move objects back along a path that the object has traveled, including its rotation.

The trick is that I would like to do these at the same rate without having the nice spline curve... I want all movement to be linear.

Vector3.MoveTowards has been working for me, but I'm having trouble getting the Quaternion.RotateTowards to do it at the same rate.

Anyone have any suggestions?

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

1 Reply

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

Answer by aldonaletto · Oct 03, 2012 at 03:27 AM

I don't know exactly what you're doing, but suppose that you have an array of positions/rotations and want the object to move at constant speed and rotate smoothly from point to point. If that's true, I suggest you to use Quaternion.Slerp and Vector3.Lerp instead: since both are controlled by a float value between 0 and 1, they will always stay in sync. In order to keep a constant speed, calculate the movement duration and vary the control variable at suitable rate.
Supposing that you want to move from pointA to pointB and rotate from rotA to rotB at the same time, you could use something like this:

 var moveSpeed: float = 5;
 
 private var moving = false;
 
 function MoveFromTo(pointA: Vector3, pointB: Vector3, rotA: Quaternion, rotB: Quaternion){
   moving = true; // it's moving now
   var dist = Vector3.Distance(pointA, pointB); // find the distance to travel
   var duration = dist / moveSpeed; // calculate the movement duration
   var t: float = 0; // t is the control variable
   while (t < 1){
     t += Time.deltaTime / duration; // t varies at calculated rate
     transform.position = Vector3.Lerp(pointA, pointB, t); // move...
     transform.rotation = Quaternion.Slerp(rotA, rotB, t); // and rotate
     yield; // let Unity free till next frame
   }
   moving = false; // movement finished
 }

The boolean variable moving is true while the object moves: when it becomes false, you can pass to the next point. In the example below, pos and rot are the positions and rotations arrays, and setting moveBack to true makes the object move back to the first position:

 var pos: Vector3[]; // positions array
 var rot: Quaternion[]; // rotations array
 var moveBack = false; // true to make move back
 var curPos: int = 0;
 
 function Update(){
   // if must moveBack and isn't at the first position...
   if (moveBack && curPos > 0){
     if (moving) return; // do nothing while moving
     var nxtPos = curPos - 1; // find the next position
     MoveFromTo(pos[curPos], pos[nxtPos], rot[curPos], rot[nxtPos]);
     curPos--;
   }
 }
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

10 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

Related Questions

How to smoothly rotate to certain directions using input axis 1 Answer

Issues with Lerps 2 Answers

How would I smooth out a Quaternion? 2 Answers

How to move an object from another position using Vector3.moveTowards precisely and slowly by swiping (mobile) 1 Answer

How to know if a gameobject is rotating? 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