- Home /
Can't do Quaternion.AngleAxis twice.
Hello! I am making a game, in which the camera will rotate around the player with mouse movements. I did this by adding the mousex and mousey movements to a vector2, (mousex being the x, mousey being the y) and then rotating the camera by using Quaternion.AngleAxis. I used two Quaternion.AngleAxis functions, one for the x, and one for the y to rotate it. When I ran the game, only the first Quaternion.AngleAxis in the code would actually rotate the camera, and it would ignore the second. Can I do this some other way? Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camMove : MonoBehaviour {
public GameObject playerBall;
Vector2 mouseLook;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update ()
{
Vector2 mouseVal = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseLook += mouseVal;
transform.rotation = Quaternion.AngleAxis(mouseLook.x, Vector3.up);
transform.rotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
transform.position = playerBall.transform.position;
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
}
}
Comment
Best Answer
Answer by Trevdevs · Aug 19, 2018 at 01:57 AM
Typed this up really quick should work I've used this kinda setup before but I didn't test it
using UnityEngine;
public class CameraLook : MonoBehaviour
{
public float lookSpeed;
public Vector2 horizontalLimiter;
public Vector2 verticalLimiter;
void Update()
{
//Get the mouse input
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
//Rotate the camera around the axis
Camera.main.transform.RotateAround(transform.position, Vector3.up, x * lookSpeed * Time.deltaTime);
Camera.main.transform.RotateAround(transform.position, Vector3.right, -y * lookSpeed * Time.deltaTime);
//Limit the rotation THIS IS OPTIONAL REMOVE IF YOU DON'T WANT IT
Vector3 cameraRotation = Camera.main.transform.eulerAngles;
cameraRotation.x = Mathf.Clamp(cameraRotation.x, horizontalLimiter.x, horizontalLimiter.y);
cameraRotation.y = Mathf.Clamp(cameraRotation.y, verticalLimiter.x, verticalLimiter.y);
cameraRotation.z = 0;
Camera.main.transform.eulerAngles = cameraRotation;
}
}