- Home /
Smooth Camera Rotation relative to World [SOLVED]
I've managed to create a simple Rotation Camera like in Super Robot Wars V game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotation : MonoBehaviour
{
public float camRotationSpeed = 90f;
float camRotation;
private void LateUpdate()
{
RotateCamera();
}
void RotateCamera()
{
camRotation = Input.GetAxis("Camera Rotate") * camRotationSpeed;
if(Input.GetButtonDown("Camera Rotate"))
{
gameObject.transform.Rotate(0, camRotation, 0, Space.World);
}
}
}
As is, the camera instantaneously rotates. I want to be able to smoothly rotate from Rotation A(current rotation) to Rotation B(Input Rotation) on a button press. I've tried Lerp and SmoothDamp but Quaternion formulas won't rotate it relative to World Space.
Answer by PprTiger · Aug 24, 2020 at 06:40 PM
With a great deal of help, I found a solution. I installed DOTween and used its scripting to fix my problem. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class CameraRotation : MonoBehaviour
{
//Camera Rotation Variables
public float camRotationSpeed = 90f;
float camRotation;
//DoTween variables
float camRotationDuration = 0.5f; //value in seconds
//Camera Rotation Check Variable
bool isCameraRotating = false;
private void LateUpdate()
{
RotateCamera();
}
void RotateCamera()
{
camRotation = Input.GetAxis("Camera Rotate") * camRotationSpeed;
if (Input.GetButtonDown("Camera Rotate"))
{
if (!isCameraRotating) //Prevents rotation button spam
{
transform.DORotate(new Vector3(0f, camRotation, 0f), camRotationDuration, RotateMode.WorldAxisAdd);
isCameraRotating = true;
Invoke("SetCameraRotateToFalse", camRotationDuration);
}
}
}
private void SetCameraRotateToFalse()
{
isCameraRotating = false;
}
}
The Rotation Check is to ensure, that the full rotation goes through before the player can make another rotation command. Otherwise, the player will be able spam the rotation until the camera becomes unhinged from its intended coordinates. Invoke, resets the cycle, allowing the player to rotate the camera again.
Your answer
Follow this Question
Related Questions
Snap the rotation of object 2 Answers
How do I rotate an object around another to have its opposite ? 0 Answers
Rotate object in the direction where it is going 1 Answer
rotate object around another object 1:1 1 Answer
instant rotate an object 3 Answers