Guest User

Untitled

a guest
Oct 5th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | Source Code | 0 0
  1. using UnityEngine;
  2.  
  3. public class CameraRelativeCharacterController : MonoBehaviour
  4. {
  5.     public float speed = 5f;
  6.     public float verticalSpeed = 5f;
  7.     public Transform cameraTransform;
  8.  
  9.     void Update()
  10.     {
  11.         float horizontal = Input.GetAxis("Horizontal");
  12.         float vertical = Input.GetAxis("Vertical");
  13.         bool shiftHeld = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  14.  
  15.         Vector3 movement = Vector3.zero;
  16.  
  17.         if (shiftHeld)
  18.         {
  19.             movement = Vector3.up * vertical * verticalSpeed;
  20.         }
  21.         else
  22.         {
  23.             Vector3 forward = cameraTransform.forward;
  24.             Vector3 right = cameraTransform.right;
  25.  
  26.             forward.y = 0f;
  27.             right.y = 0f;
  28.             forward.Normalize();
  29.             right.Normalize();
  30.  
  31.             movement = forward * vertical + right * horizontal;
  32.  
  33.             if (movement.magnitude > 1f) movement.Normalize();
  34.             movement *= speed;
  35.         }
  36.  
  37.         transform.Translate(movement * Time.deltaTime, Space.World);
  38.     }
  39. }
  40.  
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment