- Home /
Smooth Movement with set distance
So I want my character to move forward a set distance with a button press (this will be switched to a key press but I can manage that code switch) but right now, the character just kinda teleports instead of it being a smooth glide. I wasn't sure if I needed to add Time.deltaTime for the smooth transition or not so this is what I have so far:
UPDATE 11/16: So I changed the formatting of the code to use Vector3.Lerp but I'm still not getting a smooth movement for some reason. I even tried to adjust the speed and nothing has really worked out yet.
 using UnityEngine;
 using System.Collections;
 
 public class BattleMovement : MonoBehaviour {
     public Transform targetPos;
     public void MoveSquare () {
 
         var obj = GameObject.Find ("GameObject");
         targetPos = obj.transform;
         targetPos.transform.Translate (0, 0, 100);
 
         obj.transform.position = Vector3.Lerp(obj.transform.position, 
             targetPos.position, Time.deltaTime * 2);
              
     }
 }
Answer by Namey5 · Nov 14, 2016 at 06:18 AM
Try using interpolation;
 public float transitionSpeed = 2f;
 
 public void MoveSquare ()
 {
     float x = Input.GetAxis ("Horizontal") * 15.0f;
     float z = Input.GetAxis ("Vertical") * 50.0f;
 
     GameObject obj = GameObject.Find ("GameObject");
     obj.transform.position = Vector3.Lerp (obj.transform.position, new Vector3 (0, 0, z), Time.deltaTime * transitionSpeed);
 }
Note You are using a mix of C# and JavaScript syntax, which isn't a great idea generally speaking.
It's not doing what I wanted it still. I need the Gameobject to move a set distance (z + 100) and have it move smoothly with one press of the button. With your code, it only moves a bit forward and is also now moving on the X axis, which isn't needed.
$$anonymous$$y code works but it's more of the gameobject teleports ins$$anonymous$$d of sliding to the new location.
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                