- Home /
Rotating an object in position
i have been trying to build Tetris, and i have almost everything worked out but i cannot get the piece to rotate in position; whenever i tried to rotate it, it rotated as if around another object... i don't understand why this is happening. could it be because my object is a prefab made out of multiple objects? here is the code for the movement of the blocks(i have not added the downward momentum yet).
#pragma strict
public var falling : boolean;
function Start()
{
falling = true;
}
function Update()
{
if(this.falling == true)
{
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
this.transform.position.x -= 1;
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
this.transform.position.x += 1;
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
this.transform.rotation.z += 90;
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
this.transform.position.y -= 1;
}
}
}
Answer by NikunjPopat · Jul 07, 2014 at 07:42 AM
if(Input.GetKeyDown(KeyCode.UpArrow))
{
this.transform.rotation.eularAngles.z += 90;
}
//Try this
Ok then add rigidbody to the gameobject and use AddTorque()... this will definitely will help you.
ok so now it's spinning forever around the same spot it has been spinning around since the begginging...which is not in the middle of the object...which is the problem!!
Answer by rutter · Jul 07, 2014 at 06:57 AM
You might be used to rotation that's measured by "Euler angles" (degrees of rotation around the X, Y, and Z axes). Unity can support that, but doesn't use it internally.
Unity uses quaternions to represent rotation. They're a more difficult mathematical concept, but make it easier to interpolate rotations, and avoid gimbal lock (difficulty rotating at extreme positions).
Quaternions do have values named x
, y
, and z
(plus a w
), but they don't do what you're expecting.
So, let's suppose you want a quaternion rotated 90 degrees around the z-axis:
Quaternion rot = Quaternion.Euler(0, 0, 90);
Then, we apply this rotation:
Quaternion rot = Quaternion.Euler(0, 0, 90);
transform.rotation *= rot;
As a shortcut, you can call the transform's Rotate function:
transform.Rotate(0, 0, 90);
i changed it to if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)) { this.transform.Rotate(0, 0, 90); }
as you suggested... but now it rotates around the same point see$$anonymous$$gly and starts falling sideways(i added the downward momentum btw) before i changed it, it was rotating around the same point 180 degrees and falling upwards.
Your answer
Follow this Question
Related Questions
using a custom rotation in "Instantiate" 4 Answers
Move an object toward an angle in 2d space 0 Answers
How do you flip a card with smooth rotation? 1 Answer
Instantiate as a child at position 2 Answers
Finish a rotation with cube face up. 1 Answer