First time posting here asking for help. The thing is, I already resolved my issue by using another code.
But here is the new issue, I don’t understand why my code didn’t work - and it’ll be an issue later on if I’m missusing this as is.
Scenario:
I’m making a little minigame. Several rotating cylinders with an opening in them. Every cylinder moves in a different direction, and at different speeds. I’m very happy. But then I realize my code for making their starting rotation randomized doesn’t work…
This is what I had:
[SerializeField] private Quaternion initialRotation;
[SerializeField] private Quaternion randomStartPos;
[SerializeField] private bool spinningRight;
[SerializeField] private int spinningDir;
[SerializeField] private float spinningSpeed;
private void Start()
{
//Setting and saving a random Quaternion
randomStartPos = Random.rotation;
//logic to handle if it's going left or right, and at what speed.
spinningRight = (Random.value > 0.5f);
if(!spinningRight)
{
spinningDir = -1;
}
else
{
spinningDir = 1;
}
spinningSpeed = spinningDir * Random.Range(0.3f, 1.0f);
//This is where the issue is. It won't let me use the information from the quaternion. It just sets Z to 1.
transform.rotation = new Quaternion(0,0,randomStartPos.z,0);
initialRotation = transform.rotation;
}
private void Update()
{
if(isSpinning)
Spin();
}
private void Spin()
{
{
transform.Rotate(new Vector3(0f, 0f, spinningSpeed));
}
}
I ended up using:
transform.rotation = Quaternion.Euler(0,0,Random.Range(0,360));
Mainly posting it for anyone who googles this specific thing. But for myself, I’m asking this because I wanna know what I did wrong.
Thank you.