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)
{ // timer resets at 2, allowing .5 s to do the rotating
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);
}
}
}