Rotating an object

i have been trying to rotate an object in 3d and have been using

	deltaMouseX = Input.mousePosition.x - lastframeMouseX;
	lastframeMouseX = Input.mousePosition.x;	
	deltaMouseY = Input.mousePosition.y - lastframeMouseY;
	lastframeMouseY = Input.mousePosition.y;	
	

	if(((Input.GetMouseButton(0)) || Input.GetKey(KeyCode.LeftControl) ))
	{
		transform.RotateAround(Vector3.zero,Vector3(deltaMouseY * -100, deltaMouseX*100,0), Time.deltaTime * speedRot); 
		//Debug.Log("Mouse x: " + Input.mousePosition.x + " Mouse delta x: " + deltaMouseX);
	}

This works good, but once the object is no longer in its initial position the rotation direction is correct. How do I account for this?

You might try transform.position instead of Vector3.zero. This would rotate it around it’s own position and not the origin (Vector3.zero)

transform.RotateAround(transform.position,Vector3(deltaMouseY * -100, deltaMouseX*100,0), Time.deltaTime * speedRot);

I tried that and local position. No luck :frowning:

The position is always 0,0,0 for my object, so it is the same thing. I should be using transform.position anyway so the object doesn’t have to be at 0,0,0

Basically is doesn’t allow for the tilt. Moving the mouse in a certain direction will always effect either y or x.

I’m not quite sure I fully understand what you mean by “the tilt”. If you mean that you want the mouse UP motion to be relative to the cube so that it rolls or yaws along it’s own axis this would be similar to the approach I’d use.

#pragma strict
var lastframeMouseY : float;
var lastframeMouseX : float;
var speedRot : float = 5;

function Update () 
{
var deltaMouseX : float = Input.mousePosition.x - lastframeMouseX;
var deltaMouseY = Input.mousePosition.y - lastframeMouseY;

lastframeMouseX = Input.mousePosition.x;   
lastframeMouseY = Input.mousePosition.y;   

if(((Input.GetMouseButton(0)) || Input.GetKey(KeyCode.LeftControl) ))
	{
	var rotationAmount : float = Time.deltaTime * speedRot;
	var mouseAxis : Vector3 = Vector3(deltaMouseY*rotationAmount, deltaMouseX*rotationAmount,0);
		
	//transform.RotateAround(transform.position,mouseAxis,rotationAmount);
	
	transform.Rotate (mouseAxis.x, mouseAxis.y, mouseAxis.z, Space.Self);
    }
}

Edit : CORRECTION (Script error edit)
lastframeMouseX : float = Input.mousePosition.x;
should read as
lastframeMouseX = Input.mousePosition.x;

That is exactly what I was trying to do.

Many thanks for you help and advice.