Hi, Sorry for my poor English. I'm going to create a 2D game, so I add a Cube into game, and add RigidBody in it. hope it only rotat around X and Y axis.
How can I do that?
Hi, Sorry for my poor English. I'm going to create a 2D game, so I add a Cube into game, and add RigidBody in it. hope it only rotat around X and Y axis.
How can I do that?
Since you are using a rigidbody system to control you object. A configurable joint would make sense here. I know they are OVERWHELMING, but you only need to change 1 setting:
Angular ZMotion = Locked.
If you are making a 2D game I presume you only want it to rotate about one axis, namely the one toward the camera. That is the problem I am currently facing. I'm not sure if there is a better way but this is what I am currently doing:
if (transform.up != Vector3.up)
{
transform.LookAt(transform.position + transform.forward, Vector3.up);
}
When the object's 'up' is not the world up then make the objact 'look at' the current direction it is facing and reset the up to the world up.
Hope this helps.
Thank you Petroz, I found these codes work for me:
public var forceBetweenXYZ:String="xy";
private var Axis:Vector3;//desired depth
private var Rotation:Vector3;//desired rotation
function Awake()
{
Axis=transform.position;//defines the starting X position as the desired X position
Rotation=transform.eulerAngles;//defines the starting rotation as the desired rotation
}
function Update()
{//each frame
var spinforce:Vector3=gameObject.rigidbody.angularVelocity;//is the rotational force on this frame
var place:Vector3=gameObject.transform.position;//is the target position in world space on this frame
if(forceBetweenXYZ=="x")
{
if(place.x!=Axis.x)
{//if the current X position IS NOT the desired X position then...
transform.position.x=Axis.x;//go to the desired X position
}
gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
gameObject.rigidbody.angularVelocity.z=0;//stop rotating around z axis
gameObject.transform.rotation.y=Rotation.y;//resets it to the desired y rotation
gameObject.transform.rotation.z=Rotation.z;//resets it to the desired y rotation
}
else if(forceBetweenXYZ=="y")
{
if(place.y!=Axis.y)
{//if the current X position IS NOT the desired X position then...
transform.position.y=Axis.y;//go to the desired X position
}
gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
gameObject.rigidbody.angularVelocity.x=0;//stop rotating around z axis
gameObject.transform.rotation.x=Rotation.x;//resets it to the desired y rotation
gameObject.transform.rotation.z=Rotation.z;//resets it to the desired y rotation
}
else
{
if(place.z!=Axis.z)
{//if the current X position IS NOT the desired X position then...
transform.position.z=Axis.z;//go to the desired X position
}
gameObject.rigidbody.angularVelocity.y=0;//stop rotating around y axis
gameObject.rigidbody.angularVelocity.x=0;//stop rotating around z axis
gameObject.transform.rotation.y=Rotation.y;//resets it to the desired y rotation
gameObject.transform.rotation.x=Rotation.x;//resets it to the desired y rotation
if(forceBetweenXYZ!="z")
{
print("FORCING Z BY DEFAULT, USE X, Y, OR Z LOWERCASE");
}
}
}