hi all,
i’m using a simple piece of code to tilt my camera on Z axis and want to be able to reset to 0 on keystoke at runtime
it work but work but not only on Z axis, i can’t figure how to restrain it to Z and leave X and Y be
i have tried transform.rotate instead but never achieve my goal
any ideas ?
code below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraTilt : MonoBehaviour
{
public float tiltSpeed = 25f;
void Update()
{
if (Input.GetKey(KeyCode.Delete))
transform.Rotate(-Vector3.back * tiltSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.End))
transform.Rotate(Vector3.back * tiltSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.Insert))
Camera.main.transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
whel it does’nt event really work with quaternion euler, it reset my cam weirdly and compromise further move in the good direction…
Hello Jexmatex!
To achieve such a thing, you need to use the Euler angles (they’re the values you see in the Inspector). A Quaternion gives you a representation of the rotation, not the actual values.
Try something like this, it should do the trick 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraTilt : MonoBehaviour
{
public float tiltSpeed = 25f;
float currentRotX; //Value of the camera's current rotation.x value
float currentRotY; //Value of the camera's current rotation.y value
void Update()
{
currentRotX = this.transform.rotation.eulerAngles.x; //Stocking here the X value, which means you can move the camera while ingame, it'll still work
currentRotY = this.transform.rotation.eulerAngles.y; //Stocking here the Y value
if (Input.GetKey(KeyCode.Delete))
transform.Rotate(-Vector3.back * tiltSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.End))
transform.Rotate(Vector3.back * tiltSpeed * Time.deltaTime);
if (Input.GetKey(KeyCode.Insert))
Camera.main.transform.rotation = Quaternion.Euler(currentRotX, currentRotY, 0); //We only reset the rotation on the Z axis
}
}
1 Like
Dude you saved my day !
work fine, thanks a lot !
1 Like