- Home /
Question by
Manacraft05 · Apr 13, 2020 at 10:17 AM ·
rotationquaternioncamera rotateswitch cameras
Save last rotation or the camera and apply it after end of event.
In my game I have to cameras, and when I switch to the "2d" one ( player.is2D) I want the 3d camera to go back to the x rotation axis it had before: here's my code :
using UnityEngine;
using System.Collections;
public class rotation : MonoBehaviour {
[Range(1f, 10f)]
public float Sensitivity;
public float Rotation_Smoothness; //Believe it or not, adjusting this before anything else is the best way to go.
public float Resulting_Value_from_Input_x;
public float Resulting_Value_from_Input_y;
private Quaternion Quaternion_Rotate_From;
private Quaternion Quaternion_Rotate_To_x;
private Quaternion Quaternion_Rotate_To_y;
public GameObject pivot;
switchdimension player;
// Use this for initialization
public void Start()
{
player = GetComponent<switchdimension>();
}
public void Set (float sensi) {
Sensitivity = sensi;
Debug.Log(Sensitivity);
}
void Update () {
if (FindObjectOfType<PauseMenu>().InvertYAxis == false)
{
Resulting_Value_from_Input_x += Input.GetAxis("Mouse X") * Sensitivity * 100 * Time.deltaTime + Input.GetAxis("HorizontalCam") * Sensitivity * 100 * Time.deltaTime; ;
Resulting_Value_from_Input_y += Input.GetAxis("Mouse Y") * Sensitivity * 50 * Time.deltaTime + Input.GetAxis("VerticalCam") * Sensitivity * 50 * Time.deltaTime; ;
} else
{
Resulting_Value_from_Input_x += Input.GetAxis("Mouse X") * Sensitivity * 100 * Time.deltaTime + Input.GetAxis("HorizontalCam") * Sensitivity * 100 * Time.deltaTime; ;
Resulting_Value_from_Input_y -= Input.GetAxis("Mouse Y") * Sensitivity * 50 * Time.deltaTime + Input.GetAxis("VerticalCam") * Sensitivity * 50 * Time.deltaTime;
}
Quaternion_Rotate_To_y = Quaternion.Euler(Mathf.Clamp(Resulting_Value_from_Input_y, -70, 80), transform.rotation.y, 0);
Resulting_Value_from_Input_y = Mathf.Clamp(Resulting_Value_from_Input_y, -40, 70);
Quaternion_Rotate_From = transform.rotation;
Quaternion_Rotate_To_x = Quaternion.Euler (0,Resulting_Value_from_Input_x,0);
transform.rotation = Quaternion.Lerp (Quaternion_Rotate_From, Quaternion_Rotate_To_x, Time.deltaTime*Rotation_Smoothness);
pivot.transform.localRotation = Quaternion.Lerp(pivot.transform.localRotation, Quaternion_Rotate_To_y, Time.deltaTime * Rotation_Smoothness);
}
}
Comment