• 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 SHG · Dec 23, 2015 at 12:18 AM · quaternionlerpmovetowardssmoothingdamping

Quaternion.Lerp and Vector3.MoveTowards arent smoothing

The player can interact with a door and if it's raycasting towards the interactable object and the player left clicks/right trigger it either moves or rotates the object to or from the original position/rotation.

And it does do it, but not smooth even though I'm multiplying the Time.deltaTime part by a decimal (regardless of what I multiply it by it will instantly jump to that value).

Also as for the rotation part, it glitches out a lot (rotates immediately, but not even to the desired quaternion).

 private var m_isAxisInUse = false;
  var needtag : boolean = false;
  var Interactable : GameObject;
  var toggle : boolean = false;
  var on : boolean = false;
  var animating : boolean = false;
  var animName : String = "";
  private var animStarted : boolean = false;
  var moving : boolean = false;
  private var moved : boolean = false;
  private var originDest : Vector3;
  var moveDest : Vector3;
  var moveSpeed : float = 0.1;
  var rotating : boolean = false;
  private var rotated : boolean = false;
  private var originRot : Quaternion;
  var rotDest : Quaternion;
  var rotSpeed : float = 0.1;
  var style : GUIStyle = null;
  private var difference : int = 90;
  public var Message : String = "";
  public var Message2 : String = "";
  private var isViewing : boolean = false;
  var SoundEffect : AudioSource;
  var usingMultiClips : boolean = false;
  var Clip1 : AudioClip;
  var Clip2 : AudioClip;
  
  function Start(){
      originDest = Interactable.transform.position;
      originRot = Interactable.transform.rotation;
  }
  
  function Update () {
      var $$anonymous$$t : RaycastHit;
      var fwd = transform.TransformDirection (Vector3.forward);
      
      if (Physics.Raycast (transform.position, fwd, $$anonymous$$t, 1000)) {
          if(Vector3.Distance(Interactable.transform.position, transform.position) < 4){
              if($$anonymous$$t.transform == Interactable.transform && !needtag){
                  isViewing = true;
              }
              if($$anonymous$$t.transform.tag == Interactable.tag && needtag){
                  isViewing = true;
              }   
          }
      }  
      if($$anonymous$$t.transform.tag != Interactable.tag && needtag){
          isViewing = false;
      }
      if($$anonymous$$t.transform != Interactable.transform && !needtag){
          isViewing = false;
      }
      if(isViewing == true){
          if( Input.GetAxisRaw("Interact") != 1){
              if(m_isAxisInUse == false){
                  if(toggle){
                      on = !on;
                      if(on){
                          Interactable.SetActive(true);
                          SoundEffect.clip = Clip1;
                          SoundEffect.Play();
                      }
                      else if(!on){
                          Interactable.SetActive(false);
                          SoundEffect.clip = Clip2;
                          SoundEffect.Play();
                      }
                      if(!usingMultiClips){
                          SoundEffect.Play();
                      }
                  }
                  if(animating){
                      if(!animStarted){
                          GetComponent.<Animation>().Play(animName);
                          animStarted = true;
                          SoundEffect.Play();
                      }
                  }
                  if(moving){
                      moved = !moved;
                      if(!moved){
                          StartCoroutine("Move1");
                          Debug.Log("Should be moving");
                          if(usingMultiClips){
                              SoundEffect.clip = Clip1;
                              SoundEffect.Play();
                          }
                      }
                      else if(moved){
                          StartCoroutine("Move2");
                          Debug.Log("Should be moving");
                          if(usingMultiClips){
                              SoundEffect.clip = Clip2;
                              SoundEffect.Play();
                          }
                      }
                      if(!usingMultiClips){
                          SoundEffect.Play();
                      }
                  }
                  if(rotating){
                      rotated = !rotated;
                      if(!rotated){
                          //Interactable.transform.rotation = Quaternion.Lerp(originRot, rotDest, Time.deltaTime * rotSpeed);
                          if(usingMultiClips){
                              SoundEffect.clip = Clip1;
                              SoundEffect.Play();
                          }
                      }
                      else if(rotated){
                          //Interactable.transform.rotation = Quaternion.Lerp(rotDest, originRot, Time.deltaTime * rotSpeed);
                          if(usingMultiClips){
                              SoundEffect.clip = Clip2;
                              SoundEffect.Play();
                          }
                      }
                      if(!usingMultiClips){
                          SoundEffect.Play();
                      }
                  }
                  // Call your event function here.
                  m_isAxisInUse = true;
              }
          }
          if( Input.GetAxisRaw("Interact") == 1){
              m_isAxisInUse = false;
          } 
      }
  }
  
  function Move1(){
      var step = moveSpeed * Time.deltaTime;
      Interactable.transform.position = Vector3.MoveTowards(originDest, moveDest, step);
  }
  
  function Move2(){
      var step2 = moveSpeed * Time.deltaTime;
      Interactable.transform.position = Vector3.MoveTowards(moveDest, originDest, step2);
  }
  
  function OnGUI () {
          if(isViewing == true){
              if(!toggle && animating){
                  GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message, style);
              }
              else if(toggle){
                  if(!on){
                      GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message, style);
                  }
                  else if(on){
                      GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message2, style);
                  }
              }
              if(moving){
                  if(!moved){
                      GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message, style);
                  }
                  else if(moved){
                      GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message2, style);
                  }
              }
              if(rotating){
                  if(!rotated){
                      GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message, style);
                  }
                  else if(rotated){
                      GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), Message2, style);
                  }
              }
          }
          if(isViewing == false || animStarted == false){
              GUI.Label( Rect( (Screen.width/2)-difference, 10, 200, 25 ), "", style);
          }
  }
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

Answer by allenallenallen · Dec 23, 2015 at 04:03 AM

You're using Quaternion.Lerp incorrectly. In short, any forms of Lerp in Unity is actually more like a percentage.

So the arguments go like t$$anonymous$$s: Lerp (start position/rotation, end position/rotation, percentage of completion from start to end)

Read t$$anonymous$$s: http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 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

Related Questions

Smooth swapping my game object 0 Answers

3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers

Vector3.MoveTowards and Quaternion.RotateTowards 1 Answer

Quaternion.Lerp and Vector3.MoveTowards arent smoothing 1 Answer

How would I smooth out a Quaternion? 2 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