- Home /
rotating constantly
I have made this schript:
using UnityEngine;
using System.Collections;
public class Okretanje : MonoBehaviour
{
public float speed = 50f;
void Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
transform.Rotate(Vector3.left, speed * Time.deltaTime);
}
else
{
transform.Rotate(Vector3.right, speed * Time.deltaTime);
}
}
}
It is working...It rotates object right constantly but when i press space it rotates object left only for split second and continues to rotate it right.
But i would like it to make object rotate both ways constantly. Like when i press space it rotate object left constantly and when i press space again it rotate object in opposite direction constantly. So i would actually like to make toggle(every time i press button it rotates to opposite direction).
Any help would be great since I'm very bad with code. :)
Thank you in advance. :D
Answer by KiraSensei · Feb 23, 2014 at 09:59 AM
Try this :
using UnityEngine;
using System.Collections;
public class Okretanje : MonoBehaviour
{
public float speed = 50f;
private boolean turnRight = false;
void Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if (turnRight)
{
transform.Rotate(Vector3.left, speed * Time.deltaTime);
turnRight = false;
}
else
{
transform.Rotate(Vector3.right, speed * Time.deltaTime);
turnRight = true;
}
}
}
}
Thank you for answer. :)
This is what i came up with, and i tried your way too and they do the exact same thing(they rotate object very slightly).Works great :)
using UnityEngine;
using System.Collections;
public class Okretanje : $$anonymous$$onoBehaviour
{
public float speed = 50f;
public bool $$anonymous$$onstDesno;
void Update ()
{
if (Input.Get$$anonymous$$eyDown("space")){
$$anonymous$$onstDesno = !$$anonymous$$onstDesno;
if ($$anonymous$$onstDesno == true)
{
transform.Rotate(Vector3.left , speed * Time.deltaTime);
}
else
{
transform.Rotate(Vector3.right, speed * Time.deltaTime);
}
}
}
}
Is there way i can make object keep turning until i press space(toggle)?
I think I need to change this line:
transform.Rotate(Vector3.left , speed * Time.deltaTime);
But not really sure what I should change.
private var currDirection:Vector3 = Vector3.right;
void Update ()
{
if (Input.Get$$anonymous$$eyDown("space")){
$$anonymous$$onstDesno = !$$anonymous$$onstDesno;
if ($$anonymous$$onstDesno == true)
{
currDirection = Vector3.left;
}
else
{
currDirection = Vector3.right;
}
}
transform.Rotate(currDirection , speed * Time.deltaTime);
}
Thank you for your time and help :) But still i cant make it to slowly rotate full circle. :( Is there something else i should do?
What is happening ? Is it too slow ? Increase the speed value. Or does it stop before doing 360° ?
Your answer
Follow this Question
Related Questions
Constant Force doesn't work 0 Answers
Flip over an object (smooth transition) 3 Answers
Camera toggle and position 1 Answer
Rotation Question... 2 Answers
Pysics not working as expected 1 Answer