0
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

use Screen.lockCursor in your Start() method

http://docs.unity3d.com/ScriptReference/Screen-lockCursor.html

\$\endgroup\$
  • \$\begingroup\$ then the mouse tracking no longer works. \$\endgroup\$ – Flotolk Jun 24 '14 at 17:54
  • \$\begingroup\$ I need another option. I need the mouse to still exist, but only exist in the center of the screen. \$\endgroup\$ – Flotolk Jun 24 '14 at 17:55
  • \$\begingroup\$ Put your own GUI graphic in the center of the screen, instead of the mouse cursor. It would look weird if the mouse cursor wasn't moving around anyway; you probably want it to look like a target. \$\endgroup\$ – jhocking Jun 25 '14 at 13:58
  • \$\begingroup\$ How do I do that? \$\endgroup\$ – Flotolk Jun 26 '14 at 14:24
  • 1
    \$\begingroup\$ There are a number of approaches you could take. One would be to create a GUI Texture object and place it in the center of the screen. Another would be the use the immediate mode OnGui() system. However neither approach is good for complex interfaces that are more than just displaying a couple images onscreen, so the next version of Unity (which is 4.6) is going to have a robust new GUI system. \$\endgroup\$ – jhocking Jun 26 '14 at 15:36

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.