Mouse Look..Rotating Objects...

Hey guys,

Right, I’m currently working on the controls for my game, and I must admit I’m no expert in any way… I’m actually learning as I’m going… so it makes progress very very slow at the moment…

However, what’s frustrating is that what I want to do appears to be relatively easy…

Currently what I have is a an object that has constant velocity assigned to it, so essentially, it moves by itself, what I want to do is control the direction.

I’ve applied Mouse Look to the object, which kind of does what I want it to do…

The problems at the moment is that, when the mouse is moved along the horizontal axis (in order to turn) the object rotates to a certain point…then begind rotating the other way (even though I’m still turning the same way)… so what happens is that the object is rotated the wrong way for the direction it is turning… the switching of rotation will endlessly repeat, as long as I keep turning the same way…

As I said, I’m learning as I’m going, and whilst I have a programmer handy, he’s not around at the moment… Hopefully you guys can help…

Cheers, Russ.

Are you using Input.GetAxis with the “Mouse X” axis? This axis returns the position delta (ie, the amount moved in the last frame). If you set the object’s rotation directly according to the value of Mouse X, you will find the rotation resets when the mouse is held still.

There are various ways to handle this, but the easiest is probably to add the value of Mouse X to the current rotation angle each frame.

Hi andeee, thanks for a reply.

The Mouse Look script that I currently have is set up as it is by default…which is…

using UnityEngine;
using System.Collections;

/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation

/// To make an FPS style character:
/// - Create a capsule.
/// - Add a rigid body to the capsule
/// - Add the MouseLook script to the capsule.
///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSWalker script to the capsule

/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[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 rotationX = 0F;
	float rotationY = 0F;
	
	Quaternion originalRotation = 0F;

	void Update ()
	{
		if (axes == RotationAxes.MouseXAndY)
		{
			// Read the mouse input axis
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

			rotationX = ClampAngle (rotationX, minimumX, maximumX);
			rotationY = ClampAngle (rotationY, minimumY, maximumY);
			
			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
			
			transform.localRotation = originalRotation * xQuaternion * yQuaternion;
		}
		else if (axes == RotationAxes.MouseX)
		{
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationX = ClampAngle (rotationX, minimumX, maximumX);

			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			transform.localRotation = originalRotation * xQuaternion;
		}
		else
		{
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = ClampAngle (rotationY, minimumY, maximumY);

			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
			transform.localRotation = originalRotation * yQuaternion;
		}
		if(axes == RotationAxes.MouseXAndY)
		{
			
	}
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = false;
		originalRotation = transform.localRotation;
	}
	
	public static float ClampAngle (float angle, float min, float max)
	{
		if (angle < -360F)
			angle += 360F;
		if (angle > 360F)
			angle -= 360F;
		return Mathf.Clamp (angle, min, max);
	}
}

What I would like it to do is allow me to move my 3rd person character model along the horizontal axis via mouse inputs and play an animation as it does…

I have been playing around with the Look At Mouse script, which allows me to do what I want… but it doesn’t play the animation… the script for Look At Mouse is…

// speed is the rate at which the object will rotate
var speed = 4.0;


function Start ()
{
		animation.wrapMode = WrapMode.Loop;
}

function Update () {
    // Generate a plane that intersects the transform's position with an upwards normal.
    var playerPlane = new Plane(Vector3.up, transform.position);
   
    // Generate a ray from the cursor position
    var ray = Camera.main.ScreenPointToRay (Input.GetAxis("MouseXandY");
   
    // Determine the point where the cursor ray intersects the plane.
    // This will be the point that the object must look towards to be looking at the mouse.
    // Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
    //   then find the point along that ray that meets that distance.  This will be the point
    //   to look at.
    var hitdist = 0.0;
    // If the ray is parallel to the plane, Raycast will return false.
    if (playerPlane.Raycast (ray, hitdist)) {
        // Get the point along the ray that hits the calculated distance.
        var targetPoint = ray.GetPoint(hitdist);
       
        // Determine the target rotation.  This is the rotation if the transform looks at the target point.
        var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
       
        // Smoothly rotate towards the target point.
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
    }
}

Like I said, I’m a complete n00b at this, but really want to get to grips with is, as my programmer is away at the moment and it’s holding me up!

Cheers.

Ok, so ignore the animation line from Look At Mouse…

So I’ve been reading into it. If I could adapt on the Look At Mouse script, so that it possible created a second vector3 plane, say vector3.UnitX or something… and have it calculate the distance between both Vector3 objects and have the object look at the mouse on both Axis…then that would kind of be what I need.

Ok ignore all of that…

I’ve spent the day doing some major swatting up… I’m deleted everything that refered to creating a plane and raycasting against that… it was that that was restricting my movement to only the y axis… I then found another thread which dealt with the same problem…and they did the same thing…

var rSpeed = 1.0; // Scale. Speed of the movement

function FixedUpdate ()
{
   MousePosition = Input.mousePosition;
   MousePosition.x = (Screen.height/2) - Input.mousePosition.y;
   MousePosition.y = -(Screen.width/2) + Input.mousePosition.x;
   transform.Rotate(MousePosition * Time.deltaTime * rSpeed, Space.Self);

}

it’s just this now, which gives me a great basis to start from…