- Home /
How can I smooth out the crouch movement?
I know this question has been asked before, but I didn't understand any of the answers. My crouch script works, but it is not smooth. The camera just teleports instead of smoothly moving down. Here's my code: (by the way: I cut out the part where I resize the collider and the center of the character controller and so on... for the sake of clearness)
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private bool crouch = false;
private CharacterMotor charMotor;
private CharacterController charCont;
void Start (){
charMotor = GetComponent<CharacterMotor>();
charCont = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update ()
{
if(Input.GetButtonDown("Crouch"))
{
crouch=true;
Camera.main.transform.localPosition = new Vector3(0, -crouchHeightOffset, 0);
charMotor.movement.maxForwardSpeed -= 3.4f;
charMotor.movement.maxBackwardsSpeed -= 3.4f;
charMotor.movement.maxSidewaysSpeed -= 3.4f;
}
if (Input.GetButtonUp("Crouch"))
{
crouch=false;
Camera.main.transform.localPosition = new Vector3(0,0,0);
charMotor.movement.maxForwardSpeed += 3.4f;
charMotor.movement.maxBackwardsSpeed += 3.4f;
charMotor.movement.maxSidewaysSpeed += 3.4f;
}
if (crouch)
charMotor.jumping.enabled=false; else charMotor.jumping.enabled=true;
}
}
I tried Mathf.Lerp, Mathf.SmoothDamp, Mathf herpderp, Vector3.Lerp, almost everyting I guess.... I just can't get it to work. How does this Lerp and SmoothDamp work EXACTLY? (Sorry, but I really don't understand S*** of those docs about Lerp and SmoothDamp).
I hope somebody can help me out with this :)
Answer by TonyLi · Jun 28, 2013 at 03:08 PM
You need to call SmoothDamp() repeatedly until you're within the desired threshold of your goal value.
Try something like this (untested code):
private float smoothTime = 0.3f; // Smoothly crouch/uncrouch over 0.3 sec.
private float targetY = 0; // Where we want camera local Y to be.
private float velocityY = 0; // Velocity toward targetY.
void Update() {
if (Input.GetButtonDown("Crouch")) {
crouch=true;
targetY = -crouchHeightOffset;
charMotor.movement.maxForwardSpeed -= 3.4f;
charMotor.movement.maxBackwardsSpeed -= 3.4f;
charMotor.movement.maxSidewaysSpeed -= 3.4f;
}
if (Input.GetButtonUp("Crouch")) {
crouch=false;
targetY = 0;
charMotor.movement.maxForwardSpeed += 3.4f;
charMotor.movement.maxBackwardsSpeed += 3.4f;
charMotor.movement.maxSidewaysSpeed += 3.4f;
}
// Smoothly update the camera position one step closer to where we want it.
float newY = Mathf.SmoothDamp(Camera.main.transform.localPosition.y, targetY, ref velocityY, smoothTime);
Camera.main.transform.localPosition = new Vector3(0, newY, 0);
charMotor.jumping.enabled = !crouch;
}
What happens here is that the camera floats above the player, although when it moves up it's a smooth motion hahaha... Any ideas? :) I still have to grasp how the Lerp and SmoothDamp functions ACTUALLY work...
Does it go the right direction if you change:
targetY = -crouchHeightOffset;
to:
targetY = crouchHeightOffset;
The Lerp() function returns an "average" between two values. You give it a number t between 0 and 1.
Lerp(a, b, 0) returns a.
Lerp(a, b, 1) returns b.
Lerp(a, b, 0.5) returns the value exactly between a & b.
To get a smooth transition using Lerp(), you need to keep track of t yourself and gradually change it. For example, to transition over 10 steps, you could start at t=0. Every step, add 0.1 to t, until you get to t=1.
SmoothDamp() basically keeps track of t for you, in the form of a velocity value. It uses what's known as an easing function to get you from one value to another. As your current value gets closer to the target value, the velocity starts to slow down.
Thanks a lot for your reply :D it works now, and thanks a lot for the explanations also. They should end up in the docs in my opinion...
raphu604, could you please show me how your script looked at the end?, if you still have it?, i'm having difficulties with my crouch script too:)
Your answer
Follow this Question
Related Questions
Camera zoom smoothing 1 Answer
Mathf repeat at the end? help! 1 Answer
incremental movement 2 Answers
Jump with mathf.lerp problem 2 Answers