Help With Arrow Camera Script

I have a simple code that can rotate an object up and down, and I use it on my camera

var speed = 30;


 function Update () {
 
  if (Input.GetButton("Camera Up"))
      transform.Rotate(-Vector3.right * speed * Time.deltaTime);

  if (Input.GetButton("Camera Down"))
      transform.Rotate(Vector3.right * speed * Time.deltaTime);

 }

I have been trying to implement two things

  1. Limiting the rotation so the player can’t spin it 360 degrees (I’ve seen ways to do it using Mouse axis but I don’t know how to do it with Button presses)

  2. When I’m not pressing either “camera up” or “camera down”, have the camera slide back to normal (meaning, slide the X rotation back to 0)

(Please don’t just send a link to the Transform.eulerAngles page or the Transform.localEulerAngles page. I have already read these and I still can’t figure it out. It wont be helpful)

EDIT: btw the code above is written in JavaScript

Hello JoshPriceGames,

Assuming your camera is a 1st person one (otherwise your current code cannot work) ;

  • To clamp the rotation you could do something like ;

    if (Input.GetButton(“Camera Up”))
    {
    // Define angleV as private float in class
    angleV += speed * Time.deltaTime ; // angleV -= if “Camera Down”
    // Set vertical movement limit
    angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
    // Calculate rotation
    Quaternion targetRotation = Quaternion.Euler(-angleV, 0, 0);
    // Apply
    transform.rotation = targetRotation;
    }

  • Regarding your 2nd point, nothing in your current code does that. I either comes from an other script, or your camera parenting positioning, or the physics (can’t help you much here)

I managed to get a working script, with help from Meishin.

I changed it to C#, and altered it so that It doesn’t snap the other rotation axis’ when I press up/down.

Seems to work so far :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CamUpDown : MonoBehaviour {

	public float speed = 10;
	float angleV;
	public float minVerticalAngle = -45;
	public float maxVerticalAngle = 45;


	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetButton("Camera Up"))
		{
			// Define angleV as private float in class
			angleV += speed * Time.deltaTime ; // angleV -= if "Camera Down"
			// Set vertical movement limit
			angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
			// Calculate rotation
			Quaternion targetRotation = Quaternion.Euler(-angleV, transform.eulerAngles.y, transform.eulerAngles.z);
			// Apply
			transform.rotation = targetRotation;
		}


		if (Input.GetButton("Camera Down"))
		{
			// Define angleV as private float in class
			angleV -= speed * Time.deltaTime ; // angleV -= if "Camera Down"
			// Set vertical movement limit
			angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
			// Calculate rotation
			Quaternion targetRotation = Quaternion.Euler(-angleV, transform.eulerAngles.y, transform.eulerAngles.z);
			// Apply
			transform.rotation = targetRotation;
		}
	}
}