A
A
alexstrasza2016-12-28 23:35:10
C++ / C#
alexstrasza, 2016-12-28 23:35:10

How to change position on X and Y axes depending on mouse manipulations?

There is a Player object to which the camera is attached.
The following script, which is also attached to the Player, changes the object's rotation values.

using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour
{
    public enum RotationAxes
    {
        MouseXAndY = 0, MouseX = 1, MouseY = 2
    }

    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityHor = 9.0f;
    public float sensitivityVert = 9.0f;
    public float minimumVert = -45.0f;
    public float maximumVert = 45.0f;
    private float _rotationX = 0;
    void Start()
    {
        Rigidbody body = GetComponent<Rigidbody>();
        if (body != null)
            body.freezeRotation = true;
    }


    void Update()
    {
        if (axes == RotationAxes.MouseX)
        {
            transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
        }

        else if (axes == RotationAxes.MouseY)
        {
            _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
            float rotationY = transform.localEulerAngles.y;
            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }

        else
        {
            _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
            float delta = Input.GetAxis("Mouse X") * sensitivityHor;
            float rotationY = transform.localEulerAngles.y + delta;
            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }
    }
}

My goal is to have the mouse control the POSITION value, not the ROTATION value. That is, I did not change the viewing angle of the object, but its position along the X and Y axes.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexstrasza, 2016-12-29
@alexstrasza

Issue resolved. It just needed to apply transform.Translate(c). Where c is a vector with read x and y values ​​multiplied by the speed of movement.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question