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 (How to rotate an object's X, Y, Z based on mouse movement - Questions & Answers - Unity Discussions) 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;
}