- Home /
Linear Vector3.Lerp movement?
I have part of this code plagiarized from the website.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Vector3 mousePos;
private float lastAngle;
private bool isWalking;
private float startTime;
private Vector3 startPos;
private Vector3 endPos;
private float distanceToMove;
private float timeSpentOnMove;
private float moveFraction;
void Update () {
mousePos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y - transform.position.y));
transform.LookAt (new Vector3 (mousePos.x, transform.position.y, mousePos.z));
if (Input.GetKeyDown (KeyCode.W))
{
startPos = transform.position;
endPos = mousePos;
startTime = Time.time;
distanceToMove = Vector3.Distance (startPos, endPos);
isWalking = true;
}
if (isWalking == true)
{
timeSpentOnMove = (Time.time - startTime) * moveSpeed;
moveFraction = timeSpentOnMove / distanceToMove;
transform.position = Vector3.Lerp (transform.position, endPos, moveFraction);
if (transform.position == endPos)
{
isWalking = false;
}
}
}
}
But it is a slerp type movement.
How can I make my movement Linear? (Lerp)
Comment
Based on this line of code:
transform.position = Vector3.Lerp (transform.position, endPos, moveFraction);
It sure looks like it is using regular linear interpolation. Can you expand upon what's going wrong in the game?
Your answer
Follow this Question
Related Questions
Unit rotation fails consistently on all slerp rotations after the first? 0 Answers
Problem in moving via waypoint 0 Answers
Problem with Lerp/Slerp 1 Answer
Constant Transform Lerp 3 Answers
How do I translate around a circle? 3 Answers