-1

I'm looking to have a game object rotate when the trigger on the oculus go is pressed but only then, I have the following code. Unfortunately this continuously roates even when I take my finger off the trigger,

public GameObject dummyrotate;
private bool rotate = false;
float rotationAmount = .3f;
float delaySpeed = .1f;


public void rotateAntiClockwise()

{
    StartCoroutine(SlowSpin2());
}

public IEnumerator SlowSpin2()

{
    float count = 0;
    while (count <= 90)

    {
        dummyrotate.transform.Rotate(new Vector3(0,-rotationAmount,0));
        count += rotationAmount;
        yield return new WaitForSeconds(delaySpeed);
    }
}

many thanks Jono

jono
  • 35
  • 8
  • When does this rotateAntiClockwise gets called? What is the value of rotationAmount? – Ali Kanat Mar 08 '19 at 22:02
  • I've just updated the code. 'rotateAntiClockwise' gets called by a 'Pointer down' event trigger on a game object. – jono Mar 11 '19 at 09:41

2 Answers2

0

Well the problem is once you trigger rotation it will take 30 second to complete your coroutine because of your while loop. And also when you trigger it again you start another coroutine this will go on forever like this. I am assuming rotateAntiClockwise method is called every time your trigger is pressed.

I am also assuming you want to rotate the object 90 degree in y axis you can achieve it like this:

 bool isRunning = false;
 //Make sure this variable is true when triggered and false when not
 bool isTrigger = false;
 float rotationAmount = 0.3f;
 public IEnumerator SlowSpin2()
 {
    isRunning = true;
    //This will rotate the object continuously until isTriggered is false
    while (isTriggered)
    {
        Debug.Log("Rotating");
        transform.Rotate(new Vector3(0, -rotationAmount, 0));            
        yield return null;

    }
    Debug.Log(Time.time);
    isRunning = false;
 }

This script will rotate your object from 0 to 90 slowly in 2 seconds.

Then you have to modify your rotateAntiClockwise like this so you wont start another coroutine while this one is already running.

public void rotateAntiClockwise()
{
     if(!isRunning)
         StartCoroutine(SlowSpin2());
}
Ali Kanat
  • 1,888
  • 3
  • 13
  • 23
  • thank you that does work, what I ultimately need though is for the object to continuously rotate for as long as pointer down is triggered, the moment it is not for it stop rotating. – jono Mar 11 '19 at 16:57
  • Ah okay i understand. I updated my code this way it will rotate continuously until it is not triggered. – Ali Kanat Mar 11 '19 at 17:22
0

If you just want it to rotate while the trigger is being held, just don't use a coroutine for that. Coroutines will run until they are completely done, so instead try something like this:

public GameObject dummyrotate;
private bool rotate = false;
float rotationAmount = .3f;
float delaySpeed = .1f;


void Update()
{
    SlowSpin2();
}

public void SlowSpin2()

{
    if (rotate == true) {
        dummyrotate.transform.Rotate(new Vector3(0,-rotationAmount,0));
        count += rotationAmount;
    }
}
Eric Bishop
  • 469
  • 5
  • 13
  • thank you, sorry for being slow but how do you control the bool rotate being false or true through the event trigger though? – jono Mar 12 '19 at 18:02
  • Find which input you need on [this](https://docs.unity3d.com/Manual/OculusControllers.html) site, then in Unity, go to Project Settings > Input, and put in which ever input you need, then add in this to your script: `bool rotate = Input.GetButton("someInputName")`, where the string is the same as what you called it in the Input Manager. – Eric Bishop Mar 13 '19 at 01:43