- Home /
Player Movement using lerp
I am trying to make a game in which you have to cheat in order to pass the exam, and i want to be able to move around your desk (with W, A, S, D of course) and when I release the key to come back to chair. I managed to do that using lerp, but it looks weird when it reach the max distance. I will show you the movement script and the lerp script, and a video to fully understand what I mean. The problem is that it looks very weird. My question is, is there anyway to avoid that glitchy look? Please note: I am new to this, this is my second game. The link to the video: https://www.youtube.com/watch?v=3gzHiGyK5l4&feature=youtu.be&ab_channel=ZET
Answer by unity_ek98vnTRplGj8Q · Mar 04, 2021 at 05:38 PM
Do you want that max distance still? If not just turn of the lerping while you are moving. If you do, just move the Lerping to Update. The that jittering is caused by desync between the movement in update and the movement in fixed update.
I moved the Lerp to update, and it looks the same. But how do I turn off the lerp while moving? I tried to disable it when hitting w,a,s or d, but I was not really able to do it.
Set a bool in your movement script to true whenever you are moving from an input, then get a reference to your character movement in your Lerp script and just return out of the update method if that bool is true;
Answer by UnityM0nk3y · Mar 04, 2021 at 10:11 PM
I made you a little "AI" code, that uses a "Coroutine" to move back. Switch off all your codes, only add mine to your player, and assign the "Start Position && "Speed in the inspector. I never worked in Unity3D, but tried to convert it to "3D" world space. :)
Maybe this is too advanced for what you want? :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
public float speed;
private float modifiedSpeed; //the speed we will modify to move back
public Transform startPosition; //assign in inspector -> this is most likely your chair
bool stopUserInput; // a bool to prevent any other input, and wait till the user returns
bool horizontalMove; //used in the co-routine to move you
bool veriticalMove; //used in the co-routine to move you
private Vector3 positionFromStart; //used in the co-routine to determine your orientation
private Vector3 move;
void Start()
{
transform.position = startPosition.position;
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
move = transform.right * x + transform.forward * z;
if (!stopUserInput) //If we are allowed to move
{
//If we are not at our start pos yet
if (move == new Vector3(0,0,0) && transform.position != startPosition.position)
{
stopUserInput = true;
StartCoroutine(walkBackToStartPos());
}
else
{
transform.Translate(move * speed * Time.deltaTime); //else move normally
}
}
#region Movement for Co-Routine
if (horizontalMove) //move with the set speed in the co-routine (walkBackToStartPos)
{
transform.Translate(transform.right * modifiedSpeed * Time.deltaTime);
}
if (veriticalMove)
{
transform.Translate(transform.forward * modifiedSpeed * Time.deltaTime);
}
#endregion
}
IEnumerator walkBackToStartPos()
{
if (transform.position.z < startPosition.position.z) //move up
{
modifiedSpeed = speed;
veriticalMove = true;
yield return new WaitUntil(() => transform.position.z >= startPosition.position.z);
veriticalMove = false;
if (transform.position.x < startPosition.position.x) //moveRight
{
modifiedSpeed = speed;
horizontalMove = true;
yield return new WaitUntil(() => transform.position.x >= startPosition.position.x);
horizontalMove = false;
transform.position = startPosition.position;
print("Moved Back");
stopUserInput = false;
}
else //moveLeft
{
modifiedSpeed = -speed;
horizontalMove = true;
yield return new WaitUntil(() => transform.position.x <= startPosition.position.x);
horizontalMove = false;
transform.position = startPosition.position;
print("Moved Back");
stopUserInput = false;
}
}
else //moveDown
{
modifiedSpeed = -speed;
veriticalMove = true;
yield return new WaitUntil(() => transform.position.z <= startPosition.position.z);
veriticalMove = false;
if (transform.position.x < startPosition.position.x) //moveRight
{
modifiedSpeed = speed;
horizontalMove = true;
yield return new WaitUntil(() => transform.position.x >= startPosition.position.x);
horizontalMove = false;
transform.position = startPosition.position;
print("Moved Back");
stopUserInput = false;
}
else //moveLeft
{
modifiedSpeed = -speed;
horizontalMove = true;
yield return new WaitUntil(() => transform.position.x <= startPosition.position.x);
horizontalMove = false;
transform.position = startPosition.position;
print("Moved Back");
stopUserInput = false;
}
}
}
}
Answer by Zet26 · Mar 05, 2021 at 09:20 AM
I did something else, I just placed some cubes in order to prevent me from going to far. Thank you for your support!
Your answer
Follow this Question
Related Questions
How do I add a camera bounce effect every time the player lands? 0 Answers
Camera movement help 1 Answer
[2D] Camera movement causes flickering/jittering sprites 2 Answers
2D Diablo style movement 1 Answer