Before explaining how I made the Camera rotate, I will show you the script I made for my player to move. Depending on how the player moves things can drive quaternions crazy. My motion script (extremely simple) is as follows, but the settings of Rigidbody
of the player looked like the image below:
public class Controller : MonoBehaviour
{
Rigidbody _rb;
public float speed = 20f;
void Start()
{
_rb = GetComponent<Rigidbody>();
}
void Update()
{
var x = Input.GetAxis("Horizontal");
var z = Input.GetAxis("Vertical");
_rb.MovePosition(transform.position + new Vector3(x * speed * Time.deltaTime, 0, z * speed * Time.deltaTime));
}
}
If you don’t need any specific drive, I suggest keeping it restricted.
Now, the basic camera rotation script would look:
public class CameraController : MonoBehaviour
{
public Transform player;
public float sensitivity = 10f;
// Update is called once per frame
void Update()
{
var rotationX = Input.GetAxis("Mouse X") * sensitivity * 10;
var rotationY = Input.GetAxis("Mouse Y") * sensitivity * 10;
//Rotate(eixo do player caso ele mude, ângulo de rotação, ambiente)
transform.Rotate(player.up, -Mathf.Deg2Rad * rotationX, Space.World);
transform.Rotate(player.right, Mathf.Deg2Rad * rotationY, Space.World);
}
}
If you use this script, you will notice that the camera loses its axis over time, but it is possible to refocus. Now, to have a good camera drive it is always good to utilize the Quaternion.Euler()
:
public class CameraController : MonoBehaviour
{
public Transform player;
public float sensitivityX = 5f;
public float sensitivityY = 10f;
float rotationX = 0f;
float rotationY = 0f;
// Update is called once per frame
void Update()
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);
}
}
Remember that the sensitivity in Y is the rotational motion in the X axis, while the sensitivity in the Y axis is the rotational motion in the Y axis. With a simple drive introduced:
public class CameraController : MonoBehaviour
{
public Transform player;
public float sensitivityX = 5f;
public float sensitivityY = 10f;
Vector3 offset;
float rotationX = 0f;
float rotationY = 0f;
void Start()
{
offset = player.position - transform.position + new Vector3(0, 2, 10f);
}
// Update is called once per frame
void Update()
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
transform.position = player.position + offset;
transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);
}
}
How you tried to implement the Y-axis?
– CypherPotato
I implemented the Y-axis by adding a new Vector3
private Vector3 offsetY;
I also added a value to offsetY but with the Y axis:offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;
and added to Transform.position the value of offsetY:transform.position = player.position + offsetX + offsetY;
But as I move the mouse the camera will gradually approaching the point, instead of always keeping the same distance.– João Ferreira