- Home /
Question by
Sandmanbrc · Jan 16, 2021 at 02:37 PM ·
camerascripting problemgravitythird person controller
Problem with gravity in a Third Person Controller
I have a character controller with a freecam from cinemachine and a logic I took from a tutorial so that the character always moves to where a camera is looking, but I can't make this script have a character controller gravity, there is any way to add gravity without messing with camera logic?
heres the script:
{
public CharacterController controller;
public Transform cam;
public float speed;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
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);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
Thanks!
Comment
Your answer
Follow this Question
Related Questions
Import Packages - Characters 0 Answers
Player not Moving in Test,Debugging not Working Correctly 0 Answers
walking inside curved surface 1 Answer