- Home /
simple 360 degree Cube-rotation around y axis
Hi guys,
My Problem - i want to implement simple 360d. Cube -Rotation around Y-axis when i press the spacebar (so this Cube rotates itself only once when i press the spacebar and then stops) . How can i do this?
Heres my current code:
void Start () {
mSpeed = 3f;
RB = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
transform.Translate(mSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f,mSpeed*Input.GetAxis("Vertical")*Time.deltaTime);
//transform.Rotate(Vector3.up, 100 * Time.deltaTime);
if (Input.GetButtonDown ("Jump"))
{
RB.velocity = new Vector3 (0,5,0);
}
}
i want to see one simple 360 rotation of my cube around y axis
Answer by doVa09 · Mar 22, 2017 at 01:08 AM
Just do this:
void Update()
{
if (Input.GetButtonDown ("Jump"))
transform.Rotate(Vector3.up * mSpeed * Time.deltaTime);
}
And if you want it to rotate while Space is pressed and stops when Space is released then just make
if (Input.GetButton("Jump"))
It makes no sense to use deltaTime in an "xxxDown" event. It's a one time event and nothing continuous. If you use GetButton it of course is required.
Answer by Spidlee · Mar 22, 2017 at 03:06 AM
Very roughly, maybe this can help you:
bool isRotAnimPlaying = false;
float mSpeed = 3f;
float currRot = 0f;
const float rotMax = 360f;
...
void Update ()
{
// toggle rot anim state
if(Input.GetButtonDown("Jump"))
{
isRotAnimPlaying = !isRotAnimPlaying;
// reset rot tracker to allow for new rot animation
currRot = 0f;
}
// play animation
if(isRotAnimPlaying)
{
float step = mSpeed * Time.deltaTime;
// rotate object
transform.Rotate(Vector3.up * step);
// increment amount of rotation done
currRot += step;
// reached rot destination, stop animation
if(currRot >= rotMax)
{
isRotAnimPlaying = false;
// reset rot tracker to allow for new rot animation
currRot = 0f;
}
}
}
It's not precise in terms of 100% stop at 360 degrees, but it should get the desired effect you want.
Your answer
Follow this Question
Related Questions
How to Rotate Plane of Cube Around its Center? (Rotate Vectors Around Point/Axis/Direction) 1 Answer
Rotate object of certain degree on button press 1 Answer
Build rotation tool for level editor 0 Answers
CAR ROTATING HELP 2 Answers
How to find or compute or look for overall/max RPM by simply checking for rotation? 1 Answer