C# - How to make an orbital camera around the player?

Asked

Viewed 442 times

2

I’m trying to make a Storyteller game, and I want to make a camera similar to the one in the game Life Is Strange, but the camera only rotates along the X axis.

    using UnityEngine;
 using System.Collections;

 public class MouseOrbit : MonoBehaviour {

     public float turnSpeed = 4.0f;
     public Transform player;

     public float height = 1f;
     public float distance = 2f;

     private Vector3 offsetX;


     void Start () {

         offsetX = new Vector3(0, height, distance);

     }

     void LateUpdate()
     {
         offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;


         transform.position = player.position + offsetX; 
         transform.LookAt(player.position);
     }
 }

I tried to add an offsetY but still without the intended result.

What changes in code can I make to get the camera to start rotating along the Y-axis?

  • How you tried to implement the Y-axis?

  • 1

    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.

1 answer

0

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. inserir a descrição da imagem aqui

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);
    }
}
  • The statement "Y sensitivity is the rotation motion on the X axis, while the sensitivity on the Y axis is the rotation motion on the Y axis" ?

  • 1

    When we touch the Mouse Y we are changing our perspective with a rotation in X and not in Y. This is the idea of this phrase. https://images.app.goo.gl/mskZDFu2qcPGKa7

Browser other questions tagged

You are not signed in. Login or sign up in order to post.