So I was wondering how you could look around the scene, limiting camera rotation to around 45* and -45* on the X and Y axis
If you press keys IJKL, these keys would rotate the camera to look around.
(see “Little nightmares” for reference)
So I was wondering how you could look around the scene, limiting camera rotation to around 45* and -45* on the X and Y axis
If you press keys IJKL, these keys would rotate the camera to look around.
(see “Little nightmares” for reference)
I think you tried to use transform.Rotate but that didn’t work (at least properly). You need to assign a vector 3 to your rotation and change them indtead.
using UnityEngine;
public class CamRot : MonoBehaviour
{
public float rotspeed;
public Vector3 Rotation;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
gameObject.transform.rotation = Quaternion.Euler(Rotation.x, Rotation.y, Rotation.z);
if (Input.GetKey(KeyCode.I))
{
//or transfom.rotation
Rotation.x -= rotspeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.K))
{
//or transfom.rotation
Rotation.x += rotspeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.L))
{
//or transfom.rotation
Rotation.y += rotspeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.J))
{
//or transfom.rotation
Rotation.y -= rotspeed * Time.deltaTime;
}
if (Rotation.x < -45)
{
Rotation.x = 45;
}
if (Rotation.x > 45)
{
Rotation.x = 45;
}
if (Rotation.y > 45)
{
Rotation.y = 45;
}
if (Rotation.y < -45)
{
Rotation.y = 45;
}
}
}
If you are confused about sometihng or something you don’t understand please feel free to ask.
Hope this helps
New_Game_Ideas is on the right track but there’s an important improvement that may need making. If it’s a normal setup then the camera is a child of the player.
When you rotate left/right, you rotate the player. That’s because you want to move in the direction that you are facing. However, when you look up and down, you don’t rotate the player, you rotate the camera. If you rotate the player up or down, then transform.forward no longer works – you walk up/down. There is also the potential for falling over if the whole body is tilted at 45˚. Wre this my game, I’d use the following code, which I’d put on the player, not the camera.
using UnityEngine;
public class KeyboardLook : MonoBehaviour
{
[SerializeField] float rotspeed = 90;
float playerRot = 0;
float cameraRot = 30;
Camera cam;
void Start()
{
cam = Camera.main;
}
void Update()
{
RotateCamera();
RotatePlayer();
}
void RotateCamera()
{
if (Input.GetKey(KeyCode.I))
{
cameraRot -= rotspeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.K))
{
cameraRot += rotspeed * Time.deltaTime;
}
cameraRot = Mathf.Clamp(cameraRot, -45, 45);
cam.transform.rotation = Quaternion.Euler(new(cameraRot, 0, 0));
}
private void RotatePlayer()
{
if (Input.GetKey(KeyCode.L))
{
playerRot += rotspeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.J))
{
playerRot -= rotspeed * Time.deltaTime;
}
playerRot = Mathf.Clamp(playerRot, -45, 45);
gameObject.transform.rotation = Quaternion.Euler(new(0, playerRot, 0));
}
}