- Home /
Manipulate gameobjects, pick up & rotate
I am planning to have a gameobject that can be picked up by a mouse click and when the mouse is released, the object will fall. Also, i want to manipulate the object by pressing a key (X) to rotate the object clockwise (on the Z axis) with a changeable speed in the inspector. So far in the script i have set the axis as it is a 2d game
function Update()
{
var v = Input.GetAxisRaw("Vertical"); // v = -1 for down and +1 for up
var h = Input.GetAxisRaw("Horizontal"); // h = -1 for left and +1 for right input
if ( Input.GetKeyDown("X") )
{
//rotate
}
Many thanks
How about transform.Rotate(0.0, speed * Time.deltaTime, 0.0);
where 'speed' is a variable you declare and initialize to degrees per second you want it to rotate.
Also you probably want Input.Get$$anonymous$$ey() rather than Input.Get$$anonymous$$eyDown(). Get$$anonymous$$eyDown() only returns 'true' for a single frame. With Get$$anonymous$$ey() your object will rotate as long as the key is held down.
so could i use Input.Get$$anonymous$$ey() for the mouse click and X button? as i don't want it to be a toggle. And how would i structure this code? Say if my object was called 'Block 1' would i need to set a trigger so when the button was pressed the code will activate for said object?
There are lots of posts for drag and drop in Unity Answers. I'd start this by searching the list and looking at scripts. GetAxis() is not a good way to do dragging. GetAxis()/GetAxisRaw() return values in the range of 0.0 and 1.0, so it will be impossible to keep the object and the mouse aligned. Take a look at On$$anonymous$$ouseDown() and On$$anonymous$$ouseDrag() as ways to capture when a specific object has been click on. Another approach uses Physics.Raycast(). Depending on the nature of your game, dragging and dropping can be fairly easy to fairly difficult.
You might take a look at the DragObject script in the Unity Wiki:
http://wiki.unity3d.com/index.php?title=DragObject
If the object you want to drag have rigidbodies, you can use the DragRigidbody.js script. From Assets, select Import Package/Scripts. The DragRigidbody script will be one of the scripts imported.