- Home /
Move player back smoothly? (LERP help)
In my game I want the player to be forced back 50 units smoothly if he hits an obstacle.
Currently, the only thing that works is the Player.transform.Translate line I have, but that instantly teleports the player and is quite disorienting.
My player has both a Character Controller and Rigidbody attached, so the Rigidbody.addforce command won't work.
If someone could help me with a LERP command, I'd be grateful. I currently have one line that throws up many errors.
Player.transform.Translate = Vector3.Lerp(Player.transform.position, Player.transform.position-50,t);
Thanks for reading!
Answer by TheSOULDev · Sep 15, 2017 at 05:43 PM
You should use something like this:
Vector3 newPos = Player.transform.positon + (50 * Vector3.back);
while(Mathf.Abs((newPos - Player.transform.position).sqrMagnitude) > 0.05f)
Player.transform.position = Vector3.Lerp(Player.transform.position, newPos, t);
Player.transform.position = newPos;
I assume t is some sort of speed variable. Keep in mind it needs to be lower than 1 not to be instantaneous, and lower than 0.5f to last a significant time for the user to see.
Your answer
Follow this Question
Related Questions
What is the best way to script third person real-time action melee combat in C#? 0 Answers
2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers
Can I have a single rigidbody2d that acts as both collider and trigger? 1 Answer
Can you have different collision box type with one gameobject. 1 Answer