- Home /
Third person Character Controller slowing down when camera is pointing towards the ground
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
CharacterController cc;
public float speed = 2.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float rotSpeed = 4f;
Vector3 moveDirection = Vector3.zero;
void Start()
{
cc = GetComponent<CharacterController>();
cc.center.Set(0, .94f, 0);
cc.radius = .24f;
cc.height = 1.73f;
}
void Update()
{
if (cc.isGrounded)
{
Vector3 horizontal = Input.GetAxisRaw("Horizontal") * Camera.main.transform.parent.right;
Vector3 vertical = Input.GetAxisRaw("Vertical") * Camera.main.transform.parent.forward;
moveDirection = (horizontal + vertical).normalized;
moveDirection.y = 0;
moveDirection *= speed;
}
Vector3 targetDir = moveDirection;
if (targetDir == Vector3.zero)
targetDir = transform.forward;
targetDir.y = 0;
Quaternion wantedRot = Quaternion.LookRotation(targetDir);
Quaternion targetRot = Quaternion.Slerp(transform.rotation, wantedRot, Time.deltaTime * rotSpeed);
transform.rotation = targetRot;
moveDirection.y -= gravity * Time.deltaTime;
cc.Move(moveDirection * Time.deltaTime);
}
}
whenever i make my camera look down on the player while moving forward the character's forward speed slows down as if he's trying to change his look direction towards the ground. Any help on fixing this would be appreciated... please lol
Answer by unity_ek98vnTRplGj8Q · Dec 18, 2019 at 09:08 PM
Try swapping the order of these two lines: moveDirection = (horizontal + vertical).normalized;
and moveDirection.y = 0;
. The more your camera faces the ground, the larger moveDirection.y
will be compared to your normalized vector. If you normalize first, then chop off the y component, you will be left with a smaller movement vector, whereas if you chop off first and THEN normalize, your movement vector will always have the same length.
Answer by MercenarySed2 · Dec 19, 2019 at 02:49 AM
Figured it out. Had to make horizontal.y = 0 and vertical.y = 0