- Home /
Third person camera gets closer to the player
I have a third person controller that follows my player and the player moves forward in which ever direction my camera is facing. The problem is that when ever I start spamming the "W" and the jump button at around the same time, the camera starts to move closer to player, the camera even passes the player! to the point where he is not even on the screen, it could be 1 mile away from my camera. Not only does the camera pass the player, the player seems to be rotated to the right? or maybe it appears that way because of the close shot of the player, idk. Maybe parented the wrong things? I'll show you the playerMovement code, let me know if you need to see the camera look code.
Here's the player movement code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public CharacterController controller;
public float walkSpeed = 12f;
public float jumpHeight = 100f;
public float sprintSpeed = 18f;
public float backwardsSpeed = 4f;
public float currentSpeed = 0f;
Animator anim;
public float gravity = -9.81f;
Vector3 velocity;
Vector3 moveDirection;
public Transform groundDetector;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
}
// Update is called once per frame
void Update()
{
Move();
}
void Move()
{
isGrounded = Physics.CheckSphere(groundDetector.position, groundDistance, groundMask);
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
//Plays Walking Animation
if (Input.GetKey(KeyCode.W))
{
anim.SetInteger("movementCondition", 1);
}
else
{
anim.SetInteger("movementCondition", 0);
}
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//Jump Input
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
//Sprint if player is holding left-shift and "W" at the same time, should return to walking speed when left-shift is let go
moveDirection = new Vector3(moveX, 0, moveZ);
Debug.Log(moveDirection);
//Sprinting
if (Input.GetKey(KeyCode.LeftShift) && moveZ == 1 && isGrounded)
{
currentSpeed = sprintSpeed;
}
else
{
currentSpeed = walkSpeed;
}
//Moving Backwards
if (moveZ == -1)
{
currentSpeed = backwardsSpeed;
}
Debug.Log(currentSpeed);
Vector3 move = transform.right * moveX + transform.forward * moveZ;
controller.Move(move * currentSpeed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Here's the CameraLook Code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MouseLook : MonoBehaviour { Vector3 cameraPosition;
public float mouseSensitivity = 200f;
public float maxLookDown = 90;
public float maxLookUp = -90;
public Transform playerBody;
float xRotation = 0;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, maxLookDown, maxLookUp);
//xRotation = Mathf.Clamp(xRotation, -90, 90);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
Here's what the Hierarchy looks like. At first I programmed the game to be First Person, but then I didn't like the outcome, so I just moved the camera out of the mesh to make it look like it was third person. My "Mouse Look" script is in the Main Camera, and my "Player Movement" script is in the "First Person player", which is a capsule.