Alright so here’s the deal, I have the following script attached below that I’m using to control the movement for a third person game I’m working on. However, for some reason whenever I attach a rigidbody and a capsule collider to my player, things get weird.
What seems to happen is the character will teleport along the Y axis to wherever it was when the game started. So say the character was placed at X: 0 Y: 10 and Z: 0, and then I started walking, I would move just fine along the X and Z axis, but for as long as I’m holding down W, A, S, or D, the character will be transported up to Y: 10, until I let go of, when it will then teleport back to the ground.
The only solution I’ve found is to set the rigidbody to kinematic, but then the character doesn’t react with gravity at all, and that just defeats the whole purpose of having a rigidbody.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
public float turnSmoothVelocity;
public Transform cam;
Animator m_Animator;
Vector3 m_Movement;
Quaternion m_Rotation = Quaternion.identity;
void Start()
{
m_Animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg +cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
bool isWalking = true;
m_Animator.SetBool("IsWalking", isWalking);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
else
{
bool isWalking = false;
m_Animator.SetBool("IsWalking", isWalking);
}
}
}