I’ve been in the process of trying to workout a nice way to flip a gameobject on the X plan.
It works but I would’t say I was happy with it.
Once the rotation finishes I wanted to make sure that the rotation was in 0 or 180 degrees for some reason it ignores the eulerAngles rotation at the end and puts lets say 174.684 (for 180) and -5.316 (for 0), that’s why I used the eulerAngles to make sure they where reset in effect.
public class IconComponent : MonoBehaviour
{
private Boolean mRoll;
private Boolean mIsRolling;
private float mSpeed = 400f;
private float mRotation = 0;
public Boolean IsRolling
{
get { return mIsRolling; }
}
public void Roll()
{
if (this.IsRolling == false)
{
mRoll = ! mRoll;
mIsRolling = true;
}
}
void Awake()
{
mIsRolling = false;
mRoll = true;
mRotation = 0F;
}
void Start()
{
}
void Update()
{
if (mIsRolling == true)
if (mRoll == true)
{
if (mRotation >= 0F)
{
this.transform.Rotate((Time.deltaTime * mSpeed), 0, 0, Space.Self);
mRotation -= Time.deltaTime * mSpeed;
}
else
{
this.transform.eulerAngles = new Vector3(0F, 0, 0);
mIsRolling = false;
}
}
else
{
if (mRotation <= 180F)
{
this.transform.Rotate((Time.deltaTime * mSpeed), 0, 0, Space.Self);
mRotation += Time.deltaTime * mSpeed;
}
else
{
mIsRolling = false;
this.transform.eulerAngles = new Vector3(-180F, 0, 0);
}
}
}
}
any better way of doing this please let me know, I’ve look at a few idea’s LerpAngle, slerp, Lerp, Quaternion.RotateTowards, this.transform.eulerAngles
I just needed it to stop whe it reaches 0 or 180 degree so I can raise an event, I decide to keep away from a coroutine as there will be a few of these on the scene, I might even not put the code in this component and have it on the Game Board one routine for all icons in effect.
What exactly is being set to 174 and -5? Is it mRotation
? I tried running this script, and the rotation itself was correct, but mRotation
was off. This is because there’s some overshoot and you never set it back to the target value, like this:
else
{
this.transform.eulerAngles = new Vector3(0F, 0, 0);
mRotation = 0; //<< Like this
mIsRolling = false;
}
You’ll need to set it to 180 when it’s rotating the other way as well.
Hi Hyblademin,
My users love you now by the way.
anyways 174 and -5 is the X plan value after the rotations have happened, the rotation is spinning towards you
Setting mRotation to 0 or 180 does nothing.
I would of though setting the eulerAngles to:
this.transform.eulerAngles = new Vector3(0F, 0, 0);
// or
this.transform.eulerAngles = new Vector3(180F, 0, 0);
would of changed the values in the inspector but they don’t once the rotation from 0 to 180 is done the if (mRotation <= 180F) does this it stops at 174, not 180 I’m assuming because of Time.deltatime I tried fixedDeltaTime but that was the same so I though to make sure they are 180 or 0 respectively I’ll set eulerAngles but not changing.
I dont think rotation in 2D would be in the x axis, shouldnt it be on z axis instead?
Also, in my 2D projects, I dont set the rotation directly, I instead make them look at a certain point, like this :
public static void LookAt2D(this Transform t, Vector2 point, float offset = 0f)
{
Vector2 diff = point - (Vector2)t.position;
diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
t.rotation = Quaternion.Euler(0f, 0f, rot_z - 90 + offset);
}
This will depend on what sort of rotation you want to achieve, you can rotate in all 3 axis, but it will give you different results.
Actually I really wanted the sprite to have some sort of volume(depth), so next to the original game object sprite, I created a 3D version, not messed with 3D before.
A) - I created a cylinder, rotated it 90 degrees on the X rotation plain, so the top of the cylinder was looking at me.
B) - I added a material to this so I could change the colour.
C) - Then I added a gameobject with a sprite renderer, as a child of the cylinder I just created(flipped it vertically), this is effectively my icon on the surface of the cylinder (well not actually on the surface over it) because initially I don’t want to see the icon only when it rotates into view.
D) - I had to change to physics ray casting as the 2D version will not work on a 3D object, doesn’t look half bad, I’m not sure about the smoothness at the moment but the a prototype it’s fine.
What I meant before was that when I ran your script, it was setting the rotation correctly to 0 and 180, and it was only the variable mRotation
that wasn’t being set to exactly 0 and 180. However, I did notice some small values being set on the y- and z- axes… are you seeing this as well? It’s probably related to converting between Eulers and quaternion rotation values.
What I do know is that Unity converts the given Euler angles into a quaternion to apply to the transform, because it’s stored that way in memory. To get values for the inspector, it takes the quaternion and calculates Eulers from that. Between these two transformations, there’s probably some error. If you see some values in y and z rotation as well, it’s probably extremely close to the value you specified-- a small enough difference to be negligible. There’s some speculation involved there, though.
Are there any other scripts that modify the rotation angle in some way? Maybe you could try running the script on some object in an empty project to see if there’s something in your project that messes with the value.