Mouse orbit + Mouse look

I am trying to figure out on combining both mouse look and orbit… What I am trying to do now is, I was using standard mouse look script for enabling mouse look and it only lets me to use the X axis but I want to use my mouse look script both X and Y axis and dont know how to achieve this.

When I try to use both scripts at the same time, it works fine but the camera is not stable it shakes when I use the mouse.

Here’s the scripts:

Mouse Look:

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 the MouseLook script to the capsule.
///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
///   -> A CharacterMotor and a CharacterController component will be automatically added.

/// - 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 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);
		}
	}
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
	}
}

Mouse Orbit:

var target : Transform;
var distance = 10.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

@script AddComponentMenu("Camera-Control/Mouse Orbit")

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function LateUpdate () {
    if (target) {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 		
 		y = ClampAngle(y, yMinLimit, yMaxLimit);
 		       
        var rotation = Quaternion.Euler(y, x, 0);
        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
        
        transform.rotation = rotation;
        transform.position = position;
    }
}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

I am going to tell you the dumbest, and probably stupidest way to fix this…

In the Late updates… change this line:

if(target){

to…

if(target Input.GetButton(“Fire2”){

Oops… edit here. one is C# the other .JS, no wonder your confused.

In the Update add this just past the void Update(){
if(! Input.GetButton(“Fire2”) return;

Basically it says that if you hold the right mouse button down, one works, if you dont, the other one works.

Again, this is the dumbest way to do it, I would merge the two scripts and do all kinds of stuff to fix it, but that is not as simple as what I presented here.

Thank you for your reply BigMasterB, I tought no one is not going to answer it :slight_smile:

Anyway I got confused, I do not want to press any buttons to do that, I just want to be able to move the mouse on the Y axis in the game.

Tough, Do I need to change/edit specified lines from the mouseorbit file ?

What effect are you trying to achieve? Are you trying to make it so the object the camera is orbiting rotates at the same time as the camera orbits, like a third person shooter?

What I am trying to do is being able to look up and down when I try to use mouse. Now, It only lets me to lok left and right and I am using the mouse look script above.

Anyone ?

PLease guys… Still didnot figure it out :frowning:

The problem is that you are specifying two mouse scripts. One with the mouse at the target, the other with the mouse away from the target. Sadly, they are really both the same script with an extra translate to make it appear as if you are orbiting.

This is causing great confusion for us. Do you want an orbiting mouse, or do you want a looking mouse?

So lets look at the MouseOrbit… I will alter the code so that it makes a bit more sense and document the changes. (in red)

var target : Transform;
var distance = 10.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

function Start () {
	var angles = transform.eulerAngles;
	x = angles.y;
	y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function LateUpdate () {
	if (target) {
		x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
		y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
				
		y = ClampAngle(y, yMinLimit, yMaxLimit);
				   
		var rotation = Quaternion.Euler(y, x, 0);
		
[COLOR="red"]		// make the position of the camera the same as the target
		transform.position=target.position;
		// set the rotation according to the x and y mouse movement
		transform.rotation=Quaternion.Euler(y,x,0);
		// position it backwards by the distance used.
		transform.Translate(-Vector3.forward * distance);[/COLOR]
	}
}

Thank you for your reply but that does not work either… I am using 3rd person camera and mouse look at the moment. All I need want is to control my mouse on the X axis as well because it only lets me to use my mouse on X axis at the moment. THanks

You mean… just nothing more than telling it that Y is zero?

y = ClampAngle(y, yMinLimit, yMaxLimit);
y=0;

Not exactly… :confused: I just want to use the mouse freely like in FPS camera…

But I am trying to achieve this mouse movement using 3rd person camera and mouse look script. Putting both mouse orbit and mouse look onto my camera makes the camera shake non-stop. But it also lets me to move my mouse on the Y axis

Are you trying to accomplish as if you were sitting behind a character but be able to look around as if you were sitting behind him? (If that makes sense?)

Hi, yes I am trying to do something like that basically. But I just want to add full mouse support for my character as well so that you can look up, left and right as well. At the moment I can look left and right and all I did is, I replaced the mouse look script on my camera.

So now you want to look up and down? Or do you want to restrict that?

Yes I want to look up and for looking down, I need to restrict it a bit I think as I dont want to see under my terrain :smile:

So why don’t you use the MouseLook Script, attach it to your character so it follows behind him, then set the variables so that you have your limitations?

I am currently using the MouseLook Script but It does not lets me to move the mouse on Y axis :confused:

Tell me all the settings you used for the script.

Here’s a screenshot from the inspector

Remove the smooth follow script and see what you get.