Rotating/Spin Coin question
I successfully rotate my coin (a 3d Object with x = 90° rotation) using this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Coins : MonoBehaviour
{
//var to set the coin rotation spedd in Inspector
[SerializeField]
//1.0f rotattions/sec
private float rotateSpeed = 1.0f;
// Start is called before the first frame update
void Start()
{
//starts the Coroutine
StartCoroutine(Spin());
}
//to spin the coin.All Coroutines must have a return Type IEnumerator
private IEnumerator Spin()
{
//to rotate smootly a bit on each frame
while (true)
{
//transform.Rotate => transform function from Rotate class
//transform.up => along y axis
//Time.deltaTime => how much time since last frame
//360 * rotateSpeed * Time.deltaTime => how much i nedd to rotate each frame
transform.Rotate (transform.up, 360 * rotateSpeed * Time.deltaTime);
// to avoid eternal loop
yield return 0;
}
}
}
My problem is that the coin rotates in all 3 axis. Using Vector3.up or : //transform.Rotate (Vector3.up, 360 rotateSpeed Time.deltaTime);
//transform.Rotate (xAngle: 0, yAngle:360, zAngle:0);
//transform.Rotate (0,10,0);
does not work at all.
Thanks for any help !
do you want it to rotate on all axes or just on one? like y?
"$$anonymous$$y problem is that the coin rotates in all 3 axis. " I expect it to Rotate in just one axis, Y, as written here in my code:
//transform.up => along y axis
transform.Rotate (transform.up, 360 * rotateSpeed * Time.deltaTime);
It is important also to read these: https://docs.unity3d.com/ScriptReference/Transform-up.html https://docs.unity3d.com/ScriptReference/Vector3-up.html
Answer by dan_wipf · Mar 27, 2019 at 10:16 AM
this should work for you:
float t =0;
t += Time.DeltaTime;
Quaternion yAxis = Quaternion.Euler(0, t * speed,0);
transform.rotation += yAxis;
Your answer
Follow this Question
Related Questions
I can not modify localscale while I'm playing because of the Animator. 1 Answer
Position Child Object to center of Parent Object [SOLVED] 1 Answer
Object doesn't rotate well with camera raycasting. 0 Answers
Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 0 Answers
Camera isn't move position? Why my camera isn't change position? 0 Answers