Question by
ShadowToast · Feb 09, 2016 at 03:02 PM ·
scripting problemfps controllersmoothdamp
SmoothDamp in my FPS controller. Does not feel like it is working.
This is my FPS controller script:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 10.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float sensitivity = 8.0F;
public float turnSpeed = 0.02F;
private float moveX;
private float newmoveX;
private float DmoveX;
private float moveXv;
private float UpDownRange = 90.0f;
private float moveY;
private float newmoveY;
private float DmoveY;
private float moveYv;
private Vector3 moveDirection = Vector3.zero;
void Update()
{
//Move and Jump
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
//Look Left and Right
moveX = Input.GetAxis("Mouse X") * sensitivity;
newmoveX = Mathf.SmoothDamp(moveX, DmoveX, ref moveXv, turnSpeed);
transform.Rotate(0, newmoveX, 0);
//Look Up and Down
moveY -= Input.GetAxis("Mouse Y") * sensitivity;
Camera cam = GetComponentInChildren<Camera>();
moveY = Mathf.Clamp(moveY, -UpDownRange, UpDownRange);
newmoveY = Mathf.SmoothDamp(moveY, DmoveY, ref moveYv, turnSpeed);
cam.transform.localRotation = Quaternion.Euler(newmoveY, 0, 0);
}
}
When I change turnSpeed value to less than 10 the camera shakes (the closer to 10 the less shaking there is). Whatever I do I don't get the smooth movement that I want.
Comment