Advertisement
BenTibnam

Basic Camera in Unity (C#)

Jun 23rd, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Cam : MonoBehaviour
  7. {
  8.     [SerializeField] private GameObject TargetObject;
  9.    
  10.     [Header ("Position")]
  11.     [SerializeField] private float DeltaX;
  12.     [SerializeField] private float DeltaY;
  13.     [SerializeField] private float DeltaZ;
  14.  
  15.     [Header("Rotation")]
  16.     [SerializeField] private float RotationDeltaX;
  17.     [SerializeField] private float RotationDeltaY;
  18.  
  19.     [Header("Extra Options")]
  20.     [SerializeField] private bool LockRotation;
  21.    
  22.     private void SetPosition()
  23.     {
  24.         float CurrentX = TargetObject.transform.position.x + DeltaX;
  25.         float CurrentY = TargetObject.transform.position.y + DeltaY;
  26.         float CurrentZ = TargetObject.transform.position.z - DeltaZ;
  27.  
  28.         float CurrentRotationX, CurrentRotationY;
  29.         if (LockRotation)
  30.         {
  31.             CurrentRotationX = RotationDeltaX;
  32.             CurrentRotationY = RotationDeltaY;
  33.         }
  34.         else
  35.         {
  36.             CurrentRotationX = TargetObject.transform.rotation.eulerAngles.x + RotationDeltaX;
  37.             CurrentRotationY = TargetObject.transform.rotation.eulerAngles.y + RotationDeltaY;
  38.         }
  39.         transform.position = new Vector3(CurrentX, CurrentY, CurrentZ);
  40.         transform.rotation = Quaternion.Euler(new Vector3(CurrentRotationX, CurrentRotationY, TargetObject.transform.rotation.eulerAngles.z));
  41.     }
  42.  
  43.     // Start is called before the first frame update
  44.     void Start()
  45.     {
  46.         LockRotation = true;
  47.     }
  48.  
  49.     // Update is called once per frame
  50.     void Update()
  51.     {
  52.         SetPosition();
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement