Im trying to make my camera thats stuck to my player turn around it 45 degrees whenever i press Q or E. but for some reason i cant get it to work. im using C#.
using UnityEngine;
using System.Collections;
public class Camera : MonoBehaviour
{
int rotatespeed = 3;
int rotationstart = 90;
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start()
{
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void LateUpdate() {
transform.position = player.transform.position + offset;
}
void Update()
{
if (Input.GetKey("q"))
{
Camera.main.transform.rotation = Quaternion.Euler(x + 45 , y, z);
}
if (Input.GetKey("e"))
{
Camera.main.transform.rotation = Quaternion.Euler(x - 45, y, z);
}
}
}
You should multiply when rotating
if (Input.GetKey("q"))
{
Camera.main.transform.rotation *= Quaternion.Euler(45 , 0, 0);
}
if (Input.GetKey("e"))
{
Camera.main.transform.rotation *= Quaternion.Euler(-45, 0, 0);
}
However this make it look up and down, if you want left and right use the following
if (Input.GetKey("q"))
{
Camera.main.transform.rotation *= Quaternion.Euler(0, 45, 0);
}
if (Input.GetKey("e"))
{
Camera.main.transform.rotation *= Quaternion.Euler(0, -45, 0);
}
Important side-note
Keep in mind that GetKey
will return true while the user holds it down, which even on a very quick press will result in your camera seemingly spinning out of control due to it still being multiple frames. You most likely want to use GetKeyDown
which will only return true once every time the user presses the key
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.