Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class CameraRelativeCharacterController : MonoBehaviour
- {
- public float speed = 5f;
- public float verticalSpeed = 5f;
- public Transform cameraTransform;
- void Update()
- {
- float horizontal = Input.GetAxis("Horizontal");
- float vertical = Input.GetAxis("Vertical");
- bool shiftHeld = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
- Vector3 movement = Vector3.zero;
- if (shiftHeld)
- {
- movement = Vector3.up * vertical * verticalSpeed;
- }
- else
- {
- Vector3 forward = cameraTransform.forward;
- Vector3 right = cameraTransform.right;
- forward.y = 0f;
- right.y = 0f;
- forward.Normalize();
- right.Normalize();
- movement = forward * vertical + right * horizontal;
- if (movement.magnitude > 1f) movement.Normalize();
- movement *= speed;
- }
- transform.Translate(movement * Time.deltaTime, Space.World);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment