Lock mouse in center of screen, and still use to move camera Unity

Posted by Flotolk on Game Development See other posts from Game Development or by Flotolk
Published on 2014-06-24T17:09:40Z Indexed on 2014/08/23 22:36 UTC
Read the original article Hit count: 295

Filed under:
|
|

I am making a program from 1st person point of view. I would like the camera to be moved using the mouse, preferably using simple code, like from XNA

var center = this.Window.ClientBounds;
 MouseState newState = Mouse.GetState();


  if (Keyboard.GetState().IsKeyUp(Keys.Escape))
  {
      Mouse.SetPosition((int)center.X, (int)center.Y);
      camera.Rotation -= (newState.X - center.X) * 0.005f;
      camera.UpDown += (newState.Y - center.Y) * 0.005f;
  }

Is there any code that lets me do this in Unity, since Unity does not support XNA, I need a new library to use, and a new way to collect this input.

this is also a little tougher, since I want one object to go up and down based on if you move it the mouse up and down, and another object to be the one turning left and right.

I am also very concerned about clamping the mouse to the center of the screen, since you will be selecting items, and it is easiest to have a simple cross-hairs in the center of the screen for this purpose.


Here is the code I am using to move right now:

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Look")]

public class MouseLook : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;

public float minimumX = -360F;
public float maximumX = 360F;

public float minimumY = -60F;
public float maximumY = 60F;

float rotationY = 0F;

void Update ()
{
    if (axes == RotationAxes.MouseXAndY)
    {
        float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

        rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
        rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

        transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    }
    else if (axes == RotationAxes.MouseX)
    {
        transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    }
    else
    {
        rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
        rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

        transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
    }
    while (Input.GetKeyDown(KeyCode.Space) == true)
    {
        Screen.lockCursor = true;
    }
}

void Start ()
{
    // Make the rigid body not change rotation
    if (GetComponent<Rigidbody>())
        GetComponent<Rigidbody>().freezeRotation = true;
}
}

This code does everything except lock the mouse to the center of the screen. Screen.lockCursor = true; does not work though, since then the camera no longer moves, and the cursor does not allow you to click anything else either.

© Game Development or respective owner

Related posts about unity

Related posts about camera