Better MouseOrbit required for newbee

Forgive me if this has been asked and answered elsewhere but I cannot find a solution that works for me, I am new to unity and scripting knowledge is limited so I would appreciate so need a rather dumb ass type help on this!

I want to create a way to view an object so that the user can 360 pan/rotate around move up and down… This is my first test using MouseOrbit:

http://www.supercreative.co.uk/forums/unity/wheel/

But I would like the camera to rotate more like this flash example - http://www.supercreative.co.uk/3d/360tilt/

As you can see the car version works a little better as when you let go of mouse button and move it away the object freezes so the user can continue viewing it from that angle where my unity version continues moving even when your mouse pointer is not on the web browser screen.

Hope that makes send, really like Unity so far!

Thanks for your help,

Simon

In the MouseOrbit script, put all the code that is inside “LateUpdate()” inside “if(Input.GetKey(KeyCode.Mouse0){ }”, like this:

function LateUpdate(){
    if(Input.GetKey(KeyCode.Mouse0){
        //put all code here  
    }
}

Thanks Legend I will take a look and get this in.

Just realised the Unity Answers was maybe the place to post this!

http://answers.unity3d.com/questions/49933/better-mouseorbit-required-for-newbee

Either place, and either place would be good for an answer.

Hi Legend I had tried that but think I have done it wrong?

cough cough, code tags, not quote tags…

You took him too litterally. He was refering to the contents of only the LateUpdate function… :wink:

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(Input.GetKey(KeyCode.Mouse0){
		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);
}

Thanks BM

I did say my programming knollege was limited just a little lol, I see what he ment now however I still get a error:

Which is the new line if(Input.GetKey(KeyCode.Mouse0) {

Must just be a little somthing stupid that I am doing wrong?

Cheers
Simon

You’re missing a closing parenthesis:

if(Input.GetKey(KeyCode.Mouse0)){

Heh, sorry, I wasnt paying attention to the parenthesis, I was more concerned with getting the { and } right

Really sorry still cant get this to work ive now added the line if(Input.GetKey(KeyCode.Mouse0)){ its now not giving me a option to select the camera target?

Thanks for your help and patience on this guys, my full code:

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(Input.GetKey(KeyCode.Mouse0)){
    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);
}

Drag a scene object into the Target variable in the Inspector… :wink:

I think thats the problem BM there is no option in my inspector panel take a look my image of this screenshot

Click the arrow beside the MouseOrbit script… it will open it up… ;D

It’s because you’re stuck in a compiler error, because you’re missing a closing bracket for LateUpdate(){…

On the line before “static function ClampAngle ( …”, add a }

As such…

To find these, in the UniSciTE editor, just put your cursor at any {} or () or [ ] or whatever, it will show you where the preceding or proceeding bracket is. It makes finding this stuff a little easier.

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(Input.GetKey(KeyCode.Mouse0)){
		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);
}

Thanks for the VERY patient help on this guys you got me there in the end:

http://www.supercreative.co.uk/forums/unity/wheel_drag/

Thanks for the help.

I love it when people post the end result of a project. Well done, and nice wheel. :wink:

Glad for the help Big M hopfully get more stuff online soon, I dropped you a pm by the way!