I'm relatively new to Unity so my experience in writing code is limited.
I made a camera script that upon button press, rotates the camera on the y-axis, relative to the world space. It works, but its instantaneous. Without affecting its rotation speed or the other axes, I want to show a smooth rotation to its target axis. Here is the code:
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);
}
}
}
For these kinds of tasks, I would suggest you use tweeting libraries. These quite a few libraries on Unity Asset-Store some of them are free to use. Since I only used DoTween I can only tell you about that.
You can also achieve this by using CoRoutines as well. But they tend to be memory intensive sometimes.
Solution:
float camRotationDuration = 0.5f; //This value is in seconds
bool isCameraRotating = false;
void RotateCamera()
{
camRotation = Input.GetAxis("Camera Rotate") * camRotationSpeed;
if (Input.GetButtonDown("Camera Rotate"))
{
if (!isCameraRotating)
{
transform.DORotate(new Vector3(0, camRotation , 0), camRotationDuration);
isCameraRotating = true;
Invoke("SetCameraRotateToFalse", camRotationDuration);
}
}
}
private void SetCameraRotateToFalse()
{
isCameraRotating = false;
}
Please take a look at the DoTween documentation that I've added to the bottom of the answer in case you want to use this.
Reference:
DoTween Documentation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.