- Home /
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
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)
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
Answer by JoshPriceGames · Jun 13, 2019 at 10:15 PM
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 :)
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;
}
}
}
Answer by Meishin · Jun 13, 2019 at 07:21 AM
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 tried this code out, got "UCE0001: ';' expected. Insert a semicolon at the end." Of course, even if I added the ; , I'd get errors. $$anonymous$$y original script is in JavaScript, so if that is C# stuff, it's probably why it'ts not working
Regarding your 2nd point, nothing in your current code does that.
I know currently there is nothing in my script doing either of the things I asked, I am asking for help since I don't know how to do it :)