- Home /
 
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 hit : RaycastHit;
      var fwd = transform.TransformDirection (Vector3.forward);
      
      if (Physics.Raycast (transform.position, fwd, hit, 1000)) {
          if(Vector3.Distance(Interactable.transform.position, transform.position) < 4){
              if(hit.transform == Interactable.transform && !needtag){
                  isViewing = true;
              }
              if(hit.transform.tag == Interactable.tag && needtag){
                  isViewing = true;
              }   
          }
      }  
      if(hit.transform.tag != Interactable.tag && needtag){
          isViewing = false;
      }
      if(hit.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);
          }
  }
 
              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 this: Lerp (start position/rotation, end position/rotation, percentage of completion from start to end)
Read this: http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly
Your answer