- Home /
Question by
starpolo98 · Nov 16, 2018 at 09:55 PM ·
2dvector3variablelerpvector3.lerp
make my gameobject lerp between points more smoothly
Hy, So I made a script to make my player move along a curve made of point. The point are stored in an array Node[] Path. When ever I call the function Jump() it make s the player move along the curve. The probleme is that the movement between each point is not smooth, like if it were stoping at each point on the curve. Could someone help me making the movement fluid pls ? Here is my script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Node[] Path;
public GameObject thePlayer;
public float moveSpeed;
float timer;
public int currentNode;
static Vector3 currentPosition;
public Vector2 startPosition;
public bool jump;
public Rigidbody2D myRigidbody;
// Use this for initialization
void Start()
{
thePlayer = gameObject;
myRigidbody = GetComponent<Rigidbody2D>();
//CheckNode();
}
// Update is called once per frame
void Update()
{
if (jump)
{
timer += Time.deltaTime * moveSpeed;
if (currentNode == 0)
{
startPosition = thePlayer.transform.position;
currentPosition = Path[currentNode].transform.position;
currentNode++;
}
if (thePlayer.transform.position != currentPosition)
{
thePlayer.transform.position = Vector3.Lerp(startPosition, currentPosition, timer);
}
else if (currentNode < Path.Length - 1)
{
currentNode++;
CheckNode();
}
else
{
jump = false;
currentNode = 0;
}
}
else
{
myRigidbody.velocity = new Vector3(1, 0, 0);
}
}
public void Jump()
{
CheckNode();
jump = true;
}
void CheckNode()
{
timer = 0;
startPosition = thePlayer.transform.position;
currentPosition = Path[currentNode].transform.position;
}
}
Comment