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 jadaith · Nov 05, 2013 at 09:37 PM · c++vector3.slerp

Loop between points with Vector3.slerp

I have my gameobject moving between points just fine. The problem that I'm encountering is the speed at which they are moving between points. When it moves to the first Vector3 the first time the speed is fine but every time after that its going straight to the point. I know its because timer is set to += time.deltatime. But whenever I try to not to use delta time or mess with other speed forms it just cause my object to shake in place. Is there a way that I can just have a constant speed between each points? Here is my code.

 using UnityEngine;
 using System.Collections;
 
 public class Pendulum : MonoBehaviour {
 
     private Vector3 endPos = new Vector3 (2.0f, 1f, 1f);
     private Vector3 startPos = new Vector3(-2.0f, 1f, 1f);
     public float Timer = 0;
     
     public bool startMoving = true;
 
     
     
     void Start() 
     {
            transform.position = startPos;
     }
     void Update() 
     {
         //startMoving = true;
         if(startMoving)
         {
         Timer += Time.deltaTime;
         transform.position = Vector3.Slerp(startPos, endPos, Timer);
             
         StartCoroutine("BackTime");
         }
         
         if(startMoving == false)
         {
             Timer += Time.deltaTime ;
             transform.position = Vector3.Slerp(endPos, startPos, Timer);
             StartCoroutine("ForwardTime");
         }
        
     }
     
         IEnumerator BackTime()
         {
             yield return new WaitForSeconds(2);
             startMoving = false;
         }
     
         IEnumerator ForwardTime()
         {
             yield return new WaitForSeconds(2);
             startMoving = true;
         }
 
 
 }
 
 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TimBorquez · Nov 05, 2013 at 09:50 PM

im not sure if this is exactly what you're lookin for but i have used code like this to loop between two points at a constant speed:

 someObject.transform.position = Vector3(Mathf.PingPong(Time.time*5, 35), someObject.transform.position.y, someObject.transform.position.z);


just changing the ping pong values should effect speed and distance (im only moving it on the x in the example) (also i think it has to be in a loop or update function)

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 Professor Snake · Nov 05, 2013 at 10:33 PM

Just replace Timer with Time.deltaTime or reset Timer to 0 after one stage is finished. The third argument in Slerp is something that should range between 0 and 1 and determines the stage of the interpolation. Vector3.Slerp(x,y,0) will return x, and Vector3.Slerp(x,y,1) will return y. After the first interpolation is finished Timer's value is greater or equal to 1, making the rest of the interpolations instant.

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 Dracorat · Nov 06, 2013 at 06:20 PM

Fully documented for your reading pleasure...

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// Moves the attached transform from one Vector3 position to another and back again, 
 ///  over a specified time interval.
 /// </summary>
 public class Oscillate : MonoBehaviour {
     /// <summary>
     /// Whether we should move like a Sawtooth animation, with suddent turn arounds at the end
     ///  or like a sine wave with the ends "rounded" for smoother animation.
     /// Sine will not maintain a straight line - so if you want a straight line, stick with Sawtooth
     ///  or hard code an axis in to the result.
     /// </summary>
     public enum MovementTypes {
         Sawtooth,
         Sine
     }
 
     //Inspector Variables. These should be assigned in the inspector, but can be assigned by script at run time.
     public Vector3 StartPosition = new Vector3(-2, -2, -2);
     public Vector3 EndPosition = new Vector3(2, 2, 2);
     public float TimeToMoveBetweenPoints = 1f;
     public MovementTypes MovementType = MovementTypes.Sawtooth;
     
     //These variables are used to maintain state as the program runs.
     private float startTime;
     
     /// <summary>
     /// When the script begins, it needs to store the elapsed time in order to "zero" the animation.
     /// </summary>
     void Start () {
         this.startTime = Time.time;
     }
     
     /// <summary>
     /// Each frame, we need to check the current time and apply the oscillation to the transformation
     /// </summary>
     void Update () {
         // We "wrap" the results over TimeToMoveBetweenPoints * 2 because we move to and then move back
         //  which is twice the movement.
         float timeOffset = (Time.time - this.startTime) % (this.TimeToMoveBetweenPoints * 2);
         
         // We will need a variable to store the percent through an animation we are
         float percent = 0f;
         
         // If the time is less than or equal to TimeToMoveBetweenPoints, we are on the first
         //  "half" of oscillation - or oscillation "out" - otherwise we are on the second half
         //  - or oscillation "in"
         if(timeOffset <= TimeToMoveBetweenPoints){
             percent = timeOffset / TimeToMoveBetweenPoints;
             if(MovementTypes.Sawtooth == this.MovementType){
                 this.transform.position = Vector3.Lerp(StartPosition, EndPosition, percent);
             }else{
                 this.transform.position = Vector3.Slerp(StartPosition, EndPosition, percent);
             }
         }else{
             percent = (timeOffset - TimeToMoveBetweenPoints) / TimeToMoveBetweenPoints;
             if(MovementTypes.Sawtooth == this.MovementType)
             {
                 this.transform.position = Vector3.Lerp(EndPosition, StartPosition, percent);
             }else{
                 this.transform.position = Vector3.Slerp(EndPosition, StartPosition, percent);
             }
         }
     }
 }
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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Unable to call another gameObject's script 4 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Material doesn't have a color property '_Color' 4 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