Reputation: 3
So I'm Trying To Rotate My Coin Object to 0z if Y is above 0.17f and if its not will rotate back to 90z , the first if statement works fine but the other one keeps rotating my coin and i don't know why...? I'm Completely New To Unity and C# Lang !
Anyway Here's my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinScript : MonoBehaviour
{
public GameObject coin;
bool isRotated = false;
// Update is called once per frame
void FixedUpdate()
{
if (coin.transform.position.y > 0.17f && coin.transform.rotation.z >= 0f && !isRotated)
{
coin.transform.Rotate(coin.transform.rotation.x,coin.transform.rotation.y,-1f );
if (coin.transform.rotation.z <= 0f)
{
isRotated = true;
}
}else if (coin.transform.position.y < 0.17f && coin.transform.rotation.z <= 90f && isRotated)
{
coin.transform.Rotate(coin.transform.rotation.x,coin.transform.rotation.y,1f);
if (coin.transform.rotation.z >= 90f)
{
isRotated = false;
}
}
}
}
Upvotes: 0
Views: 323
Reputation: 90659
Transform.rotation
is a Quaternion
!
As the name suggests a Quaternion
(also see Wikipedia -> Quaternion has not three but four components x
, y
, z
and w
. All these move within the range -1
to +1
. Unless you really know exactly what you are doing you never touch these components directly.
If I understand you correctly you rather want to do something like e.g.
public class CoinScript : MonoBehaviour
{
public Transform coin;
public float anglePerSecond = 90;
private void FixedUpdate()
{
var targetRotation = coin.position.y > 0.17f ? Quaternion.identity : Quaternion.Euler(0, 0, 90);
coin.rotation = Quaternion.RotateTowards(coin.rotation, targetRotation, anglePerSecond * Time.deltaTime);
}
}
Upvotes: 1