(Fairly New Unity User)
I am making a 3rd person game and I found this tutorial that has been extremely helpful in making my game:
While it is nice that when you move the right stick it not only rotates the camera but the player as well, I would like to know if anyone can please help me add a feature where the camera rotates fully around the player when the player is not moving similar to most 3rd person games. This is what I have so far and of course this is using Unity’s new input system. Any help at all is much appreciated, thank you:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class BurnerMoveScript : MonoBehaviour
{
PlayerInput input;
Vector2 move;
Vector2 rotate;
private CharacterController controller;
public float speed;
public float rotationSpeed = 2.0f;
public Transform head;
private float currentHeadRotation = 0;
public float maxHeadRotation = 70.0f;
public float minHeadRotation = -70.0f;
private float yVelocity = 0;
public float jumpSpeed = 15.0f;
public float gravity = 30.0f;
private Vector3 moveVelocity = Vector3.zero;
void Awake()
{
controller = GetComponent<CharacterController>();
input = new PlayerInput();
input.Gameplay.MovePlayer.performed += ctx => move = ctx.ReadValue<Vector2>();
input.Gameplay.MovePlayer.canceled += ctx => move = Vector2.zero;
input.Gameplay.RotateCamera.performed += ctx => rotate = ctx.ReadValue<Vector2>();
input.Gameplay.RotateCamera.canceled += ctx => rotate = Vector2.zero;
input.Gameplay.PlayerJump.performed += ctx => Jump();
input.Gameplay.PlayerJump.canceled += ctx => Jump();
}
// Update is called once per frame
void Update()
{
Vector3 input = new Vector3(move.x, 0, move.y) * speed;
Vector2 turn = new Vector2(rotate.x, rotate.y);
yVelocity -= gravity * Time.deltaTime;
Vector3 velocity = moveVelocity + yVelocity * Vector3.up;
controller.Move(transform.TransformDirection(input * speed * Time.deltaTime + yVelocity * Vector3.up * Time.deltaTime)); //moves the player without freezing jump movement
transform.Rotate(Vector3.up, rotate.x * rotationSpeed);
currentHeadRotation = Mathf.Clamp(currentHeadRotation + rotate.y * rotationSpeed, minHeadRotation, maxHeadRotation);
head.localRotation = Quaternion.identity;
head.Rotate(Vector3.left, currentHeadRotation);
if (controller.isGrounded)
{
yVelocity = 0; //This is what stops the player from falling off an edge like a rock
}
}
void Jump()
{
if (input.Gameplay.PlayerJump.triggered && controller.isGrounded)
{
yVelocity = jumpSpeed;
}
}
void OnEnable()
{
input.Gameplay.Enable();
}
void OnDisable()
{
input.Gameplay.Disable();
}
}