SmoothDamp bug?
I've written a simple script for camera movement, but I have an issue with smooth damp. Positive targets work fine, but negative targets only partially work. It will smoothly increase the velocity but when the target is set back to zero the velocity is instantly 0, no smoothing.
public class CameraMovement : MonoBehaviour {
private float targetX = 0;
private float targetZ = 0;
private float velocityX = 0;
private float velocityZ = 0;
public float smoothTime;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButton("Forward")){
targetX = 10;
}
else if(Input.GetButton("Back")){
targetX = -10;
}
else {
targetX = 0;
}
if(Input.GetButton("Left")){
targetZ = -10;
}
else if(Input.GetButton("Right")){
targetZ = 10;
}
else{
targetZ = 0;
}
float oldX = transform.position.x;
float oldZ = transform.position.z;
float x = Mathf.SmoothDamp(oldX, oldX + targetX, ref velocityX, smoothTime);
float z = Mathf.SmoothDamp(oldZ, oldZ + targetZ, ref velocityZ, smoothTime);
transform.position = new Vector3(x, transform.position.y, z);
Debug.Log(velocityX);
}
}
that's because the target position becomes the position the object is already at. there's no distance left to smooth out. try using smoothdamp differently and calculate a target position independent from the players position.
I know this is old, but maybe can help someone in the future. I was having the exact same problem. I tried to switch $$anonymous$$athf.SmoothDamp with Vector2.SmoothDamp and it started working as expected.
Your answer
Follow this Question
Related Questions
Correctly smooth crouch script? 2 Answers
How to stop movement script on void start and resume after. 0 Answers
Operator `*=' cannot be applied to operands of type `UnityEngine.Vector3' and `method group' 1 Answer
MFPS MouseLook Issue, Namespace or Directive not found after Gaia import! 0 Answers
How do I limit an object to rotate back and forth between 2 angles? 1 Answer