- Home /
How can I rotate the camera in 90° with pressing one button?
Just as the question says. I would like to rotate the camera horizontally with 90° with pressing one button. I just can't figure it out. (I'd like to state that i'm a newbie, and I do not know much) The game is 3D, first person mode.
Answer by gonzalezc9999 · Mar 31, 2018 at 11:37 PM
Here is an example that I think might work but you will have to fill in the spots I put as camera with what you named your camera.
using UnityEngine;
Name of Script {
public Transform camera; public float speed = 90
void Update () { if(Input.GetButton("TurnCamRight")) You will have to put in the name of the button used to turn the camera { camera.rotation(0f, speed, 0f); //Turning it Right } if(Input.GetButton("TurnCamLeft")) { camera.rotation(0f, -speed, 0f); //Turning Left } } }
This is the best I can think of.
Okay, I'll try it with this. Thank you for the answer. ^^ (Btw: I've came back several times, but somewhy I didn't see your reply.)
it doesn't seem to work.
Any idea how to fix it? :) (It also says: "The member UnityEngine.Transform.rotation' cannot be used as method or delegate" and: "
Cameracontrol.camera' hides inherited member `UnityEngine.Component.camera'. Use the new keyword if hiding was intended" )
Answer by Remy_Unity · Apr 09, 2018 at 11:56 AM
Blind code written here : put this in your update function :
// Use GetButtonDown to only make the condition valid at the frame where it is pressed
if (Input.GetButtonDown("My Button Name" )
{
// Provide the camera variable the way you want, either by a public variable, or you can use Camera.main to affect the main camera of your game
camera.transform.Rotate(0, 90, 0);
}
For more information for the Rotate function look at the API : https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
Well, the error is obviously explained in the console : the variable "camera" has not been assigned.
And like I stated in the comment of my code : // Provide the camera variable the way you want, either by a public variable, or you can use Camera.main to affect the main camera of your game
Your answer
Follow this Question
Related Questions
Rotating first person camera on y axis not working? 1 Answer
FPS Camera Control rotation limits 1 Answer
Camera gets stuck when cursor is locked 0 Answers
First Person Camera looking down 1 Answer