Creating an Orbit Camera in Unity in under 5 minutes!

  Рет қаралды 1,330

FireDragonGameStudio

FireDragonGameStudio

Күн бұрын

Пікірлер: 10
@ahwendorf2
@ahwendorf2 4 ай бұрын
This is great! One question, when I try to rotate underneath the gameobject, I get a weird behavior where the camera jitters and flips. Is there a way to fix this?
@firedragongamestudio
@firedragongamestudio 4 ай бұрын
OH... good focus! that's actually a perfect example for a gimbal lock 😶‍🌫 idk why i never tested this... anyway, here is a script to avoid gimbal lock, using Quaternion for rotation, instead of Vector3 😃 using UnityEngine; public class OrbitCamera : MonoBehaviour { [SerializeField] private Transform target; [SerializeField] private float sensitivity = 5f; [SerializeField] private float orbitRadius = 5f; [SerializeField] private float minimumOrbitDistance = 2f; [SerializeField] private float maximumOrbitDistance = 10f; private float yaw; private float pitch; void Start() { yaw = transform.eulerAngles.y; pitch = transform.eulerAngles.x; } void Update() { if (Input.GetMouseButton(0)) { float mouseX = Input.GetAxis("Mouse X"); float mouseY = Input.GetAxis("Mouse Y"); yaw += mouseX * sensitivity; pitch -= mouseY * sensitivity; transform.rotation = Quaternion.Euler(pitch, yaw, 0); } orbitRadius -= Input.mouseScrollDelta.y / sensitivity; orbitRadius = Mathf.Clamp(orbitRadius, minimumOrbitDistance, maximumOrbitDistance); transform.position = target.position - transform.forward * orbitRadius; } }
@ahwendorf2
@ahwendorf2 4 ай бұрын
@@firedragongamestudio Thank you very much for your prompt and helpful reply! I did not know about gimble lock. Now it rotates all the way around on the x-axis as desired! :D However, now it is doing something else odd. When it is on the "back" (x-rotation between -90 and 90) then the side-to-side rotation is reversed. Any idea how to fix that?
@firedragongamestudio
@firedragongamestudio 4 ай бұрын
@@ahwendorf2 Just invert the raw X input, when in a certain range of rotation. Smth like this should work: using UnityEngine; public class OrbitCamera : MonoBehaviour { [SerializeField] private Transform target; [SerializeField] private float sensitivity = 5f; [SerializeField] private float orbitRadius = 5f; [SerializeField] private float minimumOrbitDistance = 2f; [SerializeField] private float maximumOrbitDistance = 10f; private float yaw; private float pitch; void Start() { yaw = transform.eulerAngles.y; pitch = transform.eulerAngles.x; } void Update() { if (Input.GetMouseButton(0)) { float mouseX = Input.GetAxis("Mouse X"); float mouseY = Input.GetAxis("Mouse Y"); pitch -= mouseY * sensitivity; bool isUpsideDown = pitch > 90f || pitch < -90f; // Invert yaw input if the camera is upside down if (isUpsideDown) { yaw -= mouseX * sensitivity; } else { yaw += mouseX * sensitivity; } transform.rotation = Quaternion.Euler(pitch, yaw, 0); } orbitRadius -= Input.mouseScrollDelta.y / sensitivity; orbitRadius = Mathf.Clamp(orbitRadius, minimumOrbitDistance, maximumOrbitDistance); transform.position = target.position - transform.forward * orbitRadius; } }
@stevendalton6952
@stevendalton6952 8 ай бұрын
I signed up for the highest tier. Can I get the script?
@firedragongamestudio
@firedragongamestudio 8 ай бұрын
Sure, check out the channel community tab. There is a post for posting Github usernames. I can share the code either directly in the comments there, or send you a link for a Repo. 🙂
@firedragongamestudio
@firedragongamestudio 7 ай бұрын
Are you still interested? You haven't sent me your Github username, for code sharing yet.
@anotherangle3dvisuals608
@anotherangle3dvisuals608 10 ай бұрын
And can you please provide us with the script? Thanks!
@firedragongamestudio
@firedragongamestudio 10 ай бұрын
As mentioned in the video, direct source code access is only available for channel members, but you can pause the video and just type it from the screen :)
@anotherangle3dvisuals608
@anotherangle3dvisuals608 10 ай бұрын
@@firedragongamestudioNo worries. Thanks. Are you perhaps available for custom c# Unity coding? Contact me, thanks.
Blender Tutorial for Complete Beginners - Part 1
17:57
Blender Guru
Рет қаралды 7 МЛН
AR Indoor Navigation from scratch in under 30 minutes with Unity 6
27:01
FireDragonGameStudio
Рет қаралды 11 М.
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
Easy Blender Piston Rig!
10:31
Coldstart Coder
Рет қаралды 34 М.
I tried a brand new AR framework!
18:58
Joshua Drewlow
Рет қаралды 1,2 М.
Unreal how to change color via widget
14:48
Owl_UA_Unreal
Рет қаралды 51
Building Runtime UI with UI Toolkit In Unity
21:35
Game Dev Guide
Рет қаралды 72 М.
Quickstart for AndroidXR in Unity in under 15 Minutes - Full Tutorial
14:06
How to use Cameras in Unity: Cinemachine Virtual Cameras Explained
14:31
Can I 100% Superliminal and Get a Refund?
23:36
Gronf
Рет қаралды 407 М.