Help with third person shooter camera/character rotation (please :))
Hi Im trying to build a 3D TPS game like Breath of the wild, and im struggling a little with the camera system. The way i would like it is for the camera, controlled by cinemachine, to detemine which direction is "forward" for the main character. IE if the camera was facing forward and i held up on the joystick, the character would walk forward, but if the camera was facing backwards and then i held the joystick, the character would turn around, orientate itself with the way the camera is facing and start walking in the opposite direction. I would also like it so the character only turns when moving, so if i want to spin the camera around and get a good look at Pseudo-Link's face or something i would be able to. Im basically trying to re-create a breath of the wild or assassins creed style camera system
using UnityEngine;
public class CharacterMovementController : MonoBehaviour
{
[SerializeField] private float speedMod = 1;
[SerializeField] private float jumpHeight = 10;
[SerializeField] private Rigidbody rb;
[SerializeField] private GameObject playerMesh;
[SerializeField] private float rotationSpeed;
[SerializeField] private float movementSmoothing;
[SerializeField] private float animationSmoothPad = 0.5f;
[SerializeField] private GameObject cam;
[SerializeField] private GameObject parentObject;
private Vector3 targetVelocity;
private Animator anim;
private Quaternion targetRotation;
private static Quaternion zero = new Quaternion(0, 0, 0, 0);
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
anim = playerMesh.GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate()
{
//Handle the actual movement & rotation
targetVelocity = new Vector3(
Input.GetAxis("Horizontal") * speedMod * Time.fixedDeltaTime,
rb.velocity.y,
Input.GetAxis("Vertical") * speedMod * Time.fixedDeltaTime);
rb.velocity = Vector3.Lerp(rb.velocity, targetVelocity, movementSmoothing);
if (rb.velocity != Vector3.zero)
{
targetRotation = Quaternion.LookRotation(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")), Vector3.up);
transform.rotation =
Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0
?
transform.rotation
:
Quaternion.Lerp(
transform.rotation,
targetRotation,
rotationSpeed * Time.fixedDeltaTime);
}
//Handle the movement animations
if (( Input.GetAxis("Horizontal") > animationSmoothPad || Input.GetAxis("Horizontal") < -animationSmoothPad) || Input.GetAxis("Vertical") > animationSmoothPad || Input.GetAxis("Vertical") < -animationSmoothPad)
{
anim.SetBool("isRunning", true);
}
else
{
anim.SetBool("isRunning", false);
}
//Handle jumping movement and trigger animations
if (Input.GetButtonDown("Jump"))
{
rb.AddForce(0, jumpHeight, 0, ForceMode.Impulse);
anim.SetTrigger("jump");
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.transform.CompareTag("ground"))
{
anim.SetTrigger("grounded");
}
}
}
Here is my code for the character controller. I know it isnt very neat but i will tidy it up after everything is functional!
Thanks so much! :D