- Home /
Question by
PNF-Unity-2021 · May 17, 2021 at 08:34 AM ·
movementwaypoints
How to move an object through a predefined waypoint path, point by point, without jumpimg?
Hi I have a waypoint and I move a sphere through it by pressing any key, but I have a problem: it jumps between the points, how can I make it moves smoothly?
using System.Collections;
using UnityEngine;
using UnityEngine;
public class controller : MonoBehaviour
{
public float t = 0f;
Vector3 _startPosition = Vector3.zero;
private Vector3 _selectPosition = Vector3.zero;
public float _distance;
public bool ismoving = false;
public Transform [] wayPoint;
public int currentWayPoint;
public Transform startPos;
void Update ()
{
if(currentWayPoint < wayPoint.Length)
{
Vector3 target = wayPoint[currentWayPoint].position; _startPosition = startPos.transform.position;
if( Input.anyKeyDown && ismoving==false )
{
ismoving = true;
currentWayPoint++;
}
if(ismoving == true)
{
gameObject.transform.position = Vector3.Lerp(_startPosition, target, t+= 0.0000005f/_distance);
if(t >= 1f) Complete();
}
}
}
void Complete ()
{
ismoving = false;
}
}
Comment
Answer by vanceagrig · Feb 27 at 05:16 PM
Have you tried gameObject.transform.position = Vector3.Lerp(_startPosition, target, t+= 0.0000005f/_distance*Time.deltaTime);
Your answer
![](https://koobas.hobune.stream/wayback/20220613033742im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting up Waypoints using coordinates for Prefabs 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Android 2d movement/waypoint script issues 0 Answers
How to make an object without Rigidbody move at the same speed as an object with Rigidbody? 1 Answer
Object moving between waypoints is jerky 0 Answers