Advertisement
vitareinforce

move camera

May 21st, 2025
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class MoveCamera : MonoBehaviour
  4. {
  5.     // Rotation speed in degrees per second
  6.     public float rotationSpeed = 90f;
  7.     // Minimum and maximum pitch angles
  8.     public float minPitch = -80f;
  9.     public float maxPitch = 80f;
  10.     private float yaw = 0f;   // Y-axis rotation
  11.     private float pitch = 0f; // X-axis rotation
  12.  
  13.     // Movement speed in units per second
  14.     public float moveSpeed = 5f;
  15.  
  16.     // Start is called once before the first execution of Update after the MonoBehaviour is created
  17.     void Start()
  18.     {
  19.         // Initialize yaw and pitch from current rotation
  20.         Vector3 euler = transform.rotation.eulerAngles;
  21.         yaw = euler.y;
  22.         pitch = euler.x;
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void Update()
  27.     {
  28.         // Get input from arrow keys
  29.         float horizontal = 0f;
  30.         float vertical = 0f;
  31.         if (Input.GetKey(KeyCode.RightArrow))
  32.         {
  33.             horizontal = 1f;
  34.         }
  35.         else if (Input.GetKey(KeyCode.LeftArrow))
  36.         {
  37.             horizontal = -1f;
  38.         }
  39.         if (Input.GetKey(KeyCode.UpArrow))
  40.         {
  41.             vertical = 1f;
  42.         }
  43.         else if (Input.GetKey(KeyCode.DownArrow))
  44.         {
  45.             vertical = -1f;
  46.         }
  47.  
  48.         // Calculate rotation deltas based on input and speed
  49.         yaw += horizontal * rotationSpeed * Time.deltaTime;
  50.         pitch -= vertical * rotationSpeed * Time.deltaTime;  // Invert so up arrow looks up
  51.         // Clamp pitch to prevent flipping
  52.         pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
  53.         // Apply rotation
  54.         transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
  55.  
  56.         // Get movement input from WASD keys
  57.         float moveForwardBack = 0f;
  58.         if (Input.GetKey(KeyCode.W))
  59.         {
  60.             moveForwardBack = 1f;
  61.         }
  62.         else if (Input.GetKey(KeyCode.S))
  63.         {
  64.             moveForwardBack = -1f;
  65.         }
  66.         float moveLeftRight = 0f;
  67.         if (Input.GetKey(KeyCode.D))
  68.         {
  69.             moveLeftRight = 1f;
  70.         }
  71.         else if (Input.GetKey(KeyCode.A))
  72.         {
  73.             moveLeftRight = -1f;
  74.         }
  75.  
  76.         Vector3 forward = transform.forward;
  77.         Vector3 right = transform.right;
  78.         // Flatten forward and right to the horizontal plane (so no flying)
  79.         forward.y = 0f;
  80.         right.y = 0f;
  81.         forward.Normalize();
  82.         right.Normalize();
  83.         Vector3 moveDirection = forward * moveForwardBack + right * moveLeftRight;
  84.         // Normalize to prevent faster diagonal movement
  85.         if (moveDirection.magnitude > 1f)
  86.         {
  87.             moveDirection.Normalize();
  88.         }
  89.         // Move the camera
  90.         transform.position += moveDirection * moveSpeed * Time.deltaTime;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement