- Home /
Camera movement (or something else in this script) causing somewhat jerky movement
Hi,
I have been developing a top down 3D game, and I just noticed a slight jerky movement where the camera jerks back a little when moving for a long period of time. This is repetitive and the time between each jitter seems to be the same every time.
Here is my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private Vector3 mousePos;
public Animator anim;
private bool canWalk;
void Update () {
mousePos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y - transform.position.y));
transform.LookAt (new Vector3 (mousePos.x, transform.position.y, mousePos.z));
if (Vector3.Distance (transform.position, mousePos) >= 2.5f)
{
canWalk = true;
}
if (Vector3.Distance (transform.position, mousePos) <= 1)
{
canWalk = false;
}
if (canWalk == true)
{
if (Input.GetKey (KeyCode.W) && GameObject.Find ("Control Manager").GetComponent <ControlManager> ().moveEnabled == true)
{
GetComponent <CharacterController> ().Move (transform.forward * moveSpeed * Time.deltaTime);
anim.SetInteger ("Direction", 1);
}
else
{
anim.SetInteger ("Direction", 0);
}
}
else
{
anim.SetInteger ("Direction", 0);
}
Vector3 playerXZ = new Vector3 (transform.position.x, Camera.main.transform.position.y, transform.position.z);
Vector3 velocity = Vector3.zero;
Camera.main.transform.position = Vector3.SmoothDamp (Camera.main.transform.position, playerXZ + Vector3.back * 3.75f, ref velocity, 0.05f);
GetComponent <CharacterController> ().Move (Vector3.up * Physics.gravity.y * Time.deltaTime);
}
}
I think it's the smoothdamp at the bottom, but I can't be sure. the smoothdamp target offset has to do with the angle of the top down camera, in case anyone wasnt aware. It's looking down at a 75 degree angle, which is 15 from looking straight down, so I moved the camera back a little so the player would be centered...
I think it's either the fact that the player is moving, stopping when close to the mouse, and moving again, or the player's camera is jerky. ANY help will be acknowledged and appreciated.