- Home /
Rotating object using mouse movement
I've been trying to code a "physics gun" from garrys mod into my game, though i can't seem to get the object rotating right. So i want the user to be able to hold down "e" when an object is grabbed to then rotate it using the mouse. Based on an earlier question (http://answers.unity3d.com/questions/10443/how-to-rotate-an-objects-x-y-z-based-on-mouse-move.html) i found out how to achieve this though it simply doesn't work for me. Nothing happens... This is a rough script i wrote to try it out, what am i doing wrong? :)
#pragma strict
var speed = 5.0f;
var toolEnabled : String = "physicsGun";
var objectSelectParticles : Transform;
private var selectedObject : Transform;
private var trackObject = false;
function Update ()
{
if (toolEnabled == "physicsGun")
{
//Raycast for the object we wish to manipulate
if (Input.GetMouseButtonDown(0))
{
var gunHit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), gunHit) && gunHit.transform.tag == "Prop")
{
//Enable the variable that is gonna alow for Update
trackObject = true;
//Make selectedObject date be = the data from raycast
selectedObject = gunHit.transform;
Instantiate(objectSelectParticles, gunHit.point, selectedObject.transform.rotation);
selectedObject.parent = transform;
//Make it kinematic
selectedObject.gameObject.rigidbody.isKinematic = true;
}
}
if (trackObject == true)
{
//THIS IS WHERE I WANT THE ROTATING TO HAPPEN.
if (Input.GetKeyDown(KeyCode.E))
{
selectedObject.transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
{
selectedObject.transform.position = Vector3.MoveTowards(selectedObject.transform.position, transform.position, 5.0f*Time.deltaTime);
}
}
//Turn everything off again
if (Input.GetMouseButtonUp(0))
{
trackObject = false;
if( selectedObject != null)
{
selectedObject.parent = null;
selectedObject.gameObject.rigidbody.isKinematic = false;
}
}
}
}
function ToolHandling (string)
{
Debug.Log("Tool = " + string);
toolEnabled = string;
}
Answer by robertbu · Jan 26, 2013 at 02:01 AM
Your problem is that you are using Input.GetKeyDown(). Use Input.GetKey() instead. GetKeyDown() only returns true in the frame that that the key was pressed.
Whoops, a little mistake when i rewrote it. It is still not working though.
I pulled the rotation code out into a separate script and it worked. Y rotation was backwards which could be fixed with a change in sign. At this point I'd either use the debugger or start sprinkling Debug.Log() statements to see the flow of the code. In particular is trackObject getting the value you think it should have.
I guess i will start doing that, thank you for helping :)
I've tried using the Debug.Log() but everything works as it should... The if statement gets called every frame... It must be something else.
Your answer
Follow this Question
Related Questions
Rotate a thrown spear to Mouse Position (2d top down game) 0 Answers
Smooth camera rotation on mobile and pc when GetMouseButton()? 0 Answers
Moving a object around with mouse, rotating it with arrow keys and placing it. 1 Answer
How to rotate an object around a fixed point so it follows the mouse cursor? 1 Answer