- Home /
How can I rule out object rotation only twice in a row on same direction ?
The object is rotating to both directions randomly. Right or left. The range is between 90 and 270.
But I want to add a rule that if the object rotated for example twice in a row on the left direction or the right direction the next random number should on the other side. So the object can rotate randomly only twice in a row in the same side. If rotated twice on the same side range the next it must be rotating randomly on the other side.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateRandom : MonoBehaviour
{
bool noTarget = true;
Quaternion qTo;
Quaternion[] qTos;
float rotateSpeed = 3.0f;
float timer = 0.0f;
private void Start()
{
qTo = Quaternion.Euler(new Vector3(0.0f, Random.Range(90.0f, 270.0f), 0.0f));
qTos = new Quaternion[100];
for(int i = 0; i < qTos.Length; i++)
{
qTos[i] = Quaternion.Euler(new Vector3(0.0f, Random.Range(90.0f, 270.0f), 0.0f));
}
}
private void Update()
{
timer += Time.deltaTime;
if (noTarget == true)
{
if (timer > 2)
{
qTo = Quaternion.Euler(new Vector3(0.0f, Random.Range(90.0f, 270.0f), 0.0f));
timer = 0.0f;
}
transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
}
}
}
I tried to add a qTos array and maybe to make that rule already in the Start or using this array to make the rule inside the Update.
Or maybe to make the rule directly already in the Euler line in the timer :
qTo = Quaternion.Euler(new Vector3(0.0f, Random.Range(90.0f, 270.0f), 0.0f));
Then I tried this but it's not working like I wanted it keep just rotating randomly like before :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateRandom : MonoBehaviour
{
bool noTarget = true;
Quaternion qTo;
float speed = 1.25f;
float rotateSpeed = 3.0f;
float timer = 0.0f;
int counter = 0;
private void Start()
{
qTo = Quaternion.Euler(new Vector3(0.0f, Random.Range(90.0f, 270.0f), 0.0f));
}
private void Update()
{
timer += Time.deltaTime;
if (noTarget == true)
{
if (timer > 2)
{
var rand = Random.Range(90.0f, 270.0f);
counter += 1;
if (rand >= 90 && rand <= 180)
{
if(counter == 2)
{
rand = Random.Range(180.0f, 270.0f);
counter = 0;
}
}
if (rand >= 270 && rand >= 180)
{
if (counter == 2)
{
rand = Random.Range(90.0f, 180.0f);
}
counter = 0;
}
qTo = Quaternion.Euler(new Vector3(0.0f, rand, 0.0f));
timer = 0.0f;
}
transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
}
}
}
I have been told to try to do it this way :
A pragmatic (though ugly) solution is to use a counter for how many times you have turned to the current direction, and use a direction variable.
This means that you first randomly determine the angle as an absolute (0..90, half of the total range) Then you determine the direction as random bool (false = left, true = right) If the direction is the same as last time, increment the counter. If the counter is greater than a threshold (in your case two), force the other direction (direction = not direction) If the new direction is different from the last direction, reset the direction counter According to the direction, calculate the final turn value and apply it to object.
Or to try this way :
You could simply add an int state = 0 and add 1 when you rotate on one side, sub 1 when you rotate on the other side
But not sure how to do it.
If someone can explain how to do it and give a code example ?
Thank you.
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i pick two randomly items from gameobject array ? 1 Answer
Script for a hue rainbow scroll on the material 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers