Rigidbody rotation using mouse

Hi,

Need little clue here.

I’m trying to achieve rigidbody rotation using mouse.

What I’m trying to do is, when I click and hold mouse middle button I should be able to rotate the object (with rigidbody) on x,y. To achieve this, when mouse middle button is clicked I’m setting isKinematic to ‘true’ and then applying rotation. Once I release the mouse middle button, I am setting isKinematic back to ‘false’ so I can make collider work.

But soon after I release the mouse button the rotation is reverting back to original value. How can I fix this?

Here is code from Update().

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	
	if (Physics.Raycast (ray, hit, 100)) {
		if(Input.GetMouseButton(2)){ 
			Debug.Log("Mouse middle button down");
			if (hit.collider.gameObject.name == "Cube") {
				var rotationX:float = Input.GetAxis("Mouse X") * sensitivityX;
				var rotationY:float = Input.GetAxis("Mouse Y") * sensitivityY;
				hit.collider.gameObject.rigidbody.isKinematic = true;
				hit.collider.gameObject.rigidbody.transform.Rotate(Vector3.right * sensitivityX * Time.deltaTime, rotationY);
				hit.collider.gameObject.rigidbody.transform.Rotate(Vector3.up * sensitivityY * Time.deltaTime, rotationX);
				
			}
		} else if (Input.GetMouseButtonUp(2))	{
			Debug.Log("Mouse middle button up");
			if (hit.collider.gameObject.name == "Cube") {				
				hit.collider.gameObject.rigidbody.isKinematic = false;	
			}
		}
	}

Please help.

Thanks Regards,
Ganesh.

Ok I am going to suggest something that may help fix your problem. It involves caching the values so they are easier to reference.

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
        var finalRotation : Quaternion;
	
	if (Physics.Raycast (ray, hit, 100)) {
		if(Input.GetMouseButton(2)){ 
			Debug.Log("Mouse middle button down");
			if (hit.collider.gameObject.name == "Cube") {
				var rotationX:float = Input.GetAxis("Mouse X") * sensitivityX;
				var rotationY:float = Input.GetAxis("Mouse Y") * sensitivityY;
				hit.collider.gameObject.rigidbody.isKinematic = true;
				hit.collider.gameObject.rigidbody.transform.Rotate(Vector3.right * sensitivityX * Time.deltaTime, rotationY);
				hit.collider.gameObject.rigidbody.transform.Rotate(Vector3.up * sensitivityY * Time.deltaTime, rotationX);

                                finalRotation = hit.collider.transform.rotation;
			}
		} else if (Input.GetMouseButtonUp(2))	{
			Debug.Log("Mouse middle button up");
			if (hit.collider.gameObject.name == "Cube") {				
				hit.collider.gameObject.rigidbody.isKinematic = false;
                                hit.colldier.transform.rotation = finalRotation;	
			}
		}
	}

Try that and let me know how it goes.

Myhi

Thanks for the reply. Unfortunately, it didn’t work.

var finalRotation : Quaternion; // [B]Is this supposed to be in Update()?[/B]
finalRotation = hit.collider.transform.rotation; // Didn't work
hit.collider.transform.rotation = finalRotation;

I also tried,

finalRotation = hit.collider.gameObject.rigidbody.transform.rotation; // Didn't work either
hit.collider.gameObject.rigidbody.transform.rotation = finalRotation; //

This gave me an idea, I’ll be trying this approach. Is there any better approach where I can
achieve rigidbody x,y movement and x,y rotation using mouse (differnt clicks ofcourse)?

Try moving the

var finalRotation : Quanternion

to outside the Update

Hi Myhijim,

I moved the declaration to outside Update(). Still didn’t work. I printed the values before and after they both return correct. But object reverts to initial rotation.

finalRotation [final rotation to cache]: (-0.2, -0.1, 0.0, 1.0)

finalRotation [final rotation from cache]: (-0.2, -0.1, 0.0, 1.0)

May be a dumb question, can I use transform.Rotate for rigidbodies?

Thanks for help.

That may have been the problem :smile:

Try this