Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float moveSpeed = 5f;
- public float rotateSpeed = 150f;
- public float gravity = -9.81f;
- private CharacterController _characterController;
- private Vector3 _velocity;
- public Animator animator;
- private bool _isWalking;
- public Camera playerCamera;
- public float cameraDistance = 5.0f; // Distance the camera stays behind the player
- public float cameraHeight = 2.0f; // Height the camera stays above the player
- public float cameraRotationSpeed = 5.0f; // Speed of camera rotation around the player
- private float _currentCameraAngle = 0f; // Current angle of the camera around the player
- private Collider _playerCollider;
- private void Start()
- {
- _characterController = GetComponent<CharacterController>();
- _playerCollider = GetComponent<Collider>(); // Get the Collider component
- }
- private void Update()
- {
- HandleMovement();
- ApplyGravity();
- HandleCamera();
- }
- private void HandleMovement()
- {
- float moveDirection = 0f;
- _isWalking = false;
- if (Input.GetKey(KeyCode.W))
- {
- _isWalking = true;
- moveDirection = 1f;
- }
- else if (Input.GetKey(KeyCode.S))
- {
- _isWalking = true;
- moveDirection = -1f;
- }
- Vector3 cameraForward = playerCamera.transform.forward;
- cameraForward.y = 0f; // Ensure the direction is only horizontal
- Vector3 move = cameraForward * moveDirection * moveSpeed * Time.deltaTime;
- Vector3 strafe = Vector3.zero;
- if (Input.GetKey(KeyCode.A))
- {
- _isWalking = true;
- strafe -= playerCamera.transform.right;
- }
- else if (Input.GetKey(KeyCode.D))
- {
- _isWalking = true;
- strafe += playerCamera.transform.right;
- }
- strafe *= moveSpeed * Time.deltaTime;
- Vector3 combinedMove = move + strafe;
- _characterController.Move(combinedMove);
- // Update the walking animation state
- if (combinedMove.magnitude > 0)
- {
- _isWalking = true;
- animator.SetBool("isRunning", true);
- }
- else
- {
- _isWalking = false;
- animator.SetBool("isRunning", false);
- }
- // Calculate the target rotation based on the combined movement direction
- if (combinedMove.magnitude > 0)
- {
- Quaternion targetRotation = Quaternion.LookRotation(combinedMove.normalized, Vector3.up);
- transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 500 * Time.deltaTime);
- }
- }
- private void ApplyGravity()
- {
- if (_characterController.isGrounded && _velocity.y < 0)
- {
- _velocity.y = 0f;
- }
- _velocity.y += gravity * Time.deltaTime;
- _characterController.Move(_velocity * Time.deltaTime);
- }
- private void HandleCamera()
- {
- if (Input.GetMouseButton(1))
- {
- float horizontal = Input.GetAxis("Mouse X") * cameraRotationSpeed;
- _currentCameraAngle += horizontal;
- }
- Quaternion cameraRotation = Quaternion.Euler(0, _currentCameraAngle, 0);
- Vector3 cameraPositionOffset = cameraRotation * new Vector3(0, 0, -cameraDistance);
- playerCamera.transform.position = transform.position + cameraPositionOffset + Vector3.up * cameraHeight;
- Vector3 lookAtPoint = CalculateLookAtPoint();
- playerCamera.transform.LookAt(lookAtPoint);
- }
- private Vector3 CalculateLookAtPoint()
- {
- if (_playerCollider != null)
- {
- Bounds bounds = _playerCollider.bounds;
- return bounds.center + Vector3.up * (bounds.extents.y * 0.75f); // Look at a point 3/4th up from the base of the player
- }
- else
- {
- return transform.position + Vector3.up * (cameraHeight / 4); // Fallback in case there is no collider
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement