1
\$\begingroup\$

I have a code that allows me to move the camera on the x-axis. I need it to move on the y-axis as well as the x.

    using UnityEngine;
using System.Collections;

public class orbit : MonoBehaviour {


    public float turnSpeed = 4.0f;
    public Transform player;

    private Vector3 offset;

    void Start () {
        offset = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);
    }

    void LateUpdate()
    {
        offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
        transform.position = player.position + offset; 
        transform.LookAt(player.position);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ try adding offset = offset + Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, transform.right) * offset; after offset=Quaternion.... \$\endgroup\$ – dnk drone.vs.drones Sep 22 '15 at 6:54
2
\$\begingroup\$

You could just do the same thing on the Y axes, like so:

void LateUpdate()
{
    offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
    offset = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.left) * offset;
    transform.position = player.position + offset; 
    transform.LookAt(player.position);
}

Or perhaps even simpler like so:

void LateUpdate()
{
    player.transform.rotation *= Quaternion.Euler(Input.GetAxis("Mouse X") * turnSpeed, Input.GetAxis("Mouse Y") * turnSpeed, 0);
}

However, those two ways 'rotate based on the current orientation/rotation' of the player.

This next one will rotate the same way, no matter how the player is orientated:

void LateUpdate()
{
    player.transform.Rotate(Vector3.up, Input.GetAxis( "Mouse X" ), Space.World);
    player.transform.Rotate(Vector3.left, Input.GetAxis( "Mouse Y" ), Space.World);
}

Hope that helps!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.