Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Slerp Problem

Discussion in 'Scripting' started by HorseIsAwesome, Apr 25, 2015.

  1. HorseIsAwesome

    HorseIsAwesome

    Joined:
    Apr 25, 2015
    Posts:
    2
    Hi,
    I am currently having a problem with a control system I made that uses Vector3.Slerp. For some reason, it is acting quite strange.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class controls : MonoBehaviour {
    6.     public float target;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if(Input.GetKeyDown (KeyCode.A) && target > -1){
    16.             target -= 1;
    17.         }
    18.  
    19.         if(Input.GetKeyDown (KeyCode.D) && target < 1){
    20.             target += 1;
    21.         }
    22.  
    23.         transform.position = Vector3.Slerp (new Vector3(transform.position.x,0,0), new Vector3 (target, 0, 0), 0.1f);
    24.         target = Mathf.Clamp (target, -1, 1);
    25.     }
    26.  
    27. }
    28.  
    I have put this code on a cube without a rigidbody, and with a box collider.
     
  2. bigSky

    bigSky

    Joined:
    Jan 31, 2011
    Posts:
    114
    It's probably the Slerp - try Lerp - Slerp will interpolate between the (changing) values between from and to, and potentially change other axis you don't intend to change. And, instead of 0.1f you might consider using Time.deltaTime * speed.
     
    HorseIsAwesome likes this.
  3. HorseIsAwesome

    HorseIsAwesome

    Joined:
    Apr 25, 2015
    Posts:
    2
    I tried the Lerp and it worked! You are a life saver!