How can I find when the object is rotating to the same direction twice in a row ?

  • Thread starter Thread starter Chocolade1972
  • Start date Start date
C

Chocolade1972

Guest
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateRandom : MonoBehaviour
{
public string currDirection;
bool noTarget = true;
Quaternion qTo;
float rotateSpeed = 3.0f;
float timer = 0.0f;
bool directions;
float rand;

private void Start()
{
qTo = Quaternion.Euler(new Vector3(0.0f, Random.Range(135.0f, 225.0f), 0.0f));

rand = Random.Range(135.0f, 225.0f);
if (rand < 180)
{
currDirection = "Right";
}

if (rand > 180)
{
currDirection = "Left";
}
}

private void Update()
{
timer += Time.deltaTime;

if (noTarget == true)
{
if (timer > 2)
{

rand = Random.Range(135.0f, 225.0f);

if (rand < 180)
{
currDirection = "Right";
}

if (rand > 180)
{
currDirection = "Left";
}

qTo = Quaternion.Euler(new Vector3(0.0f, rand, 0.0f));

timer = 0.0f;
}
transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
}
}
}



This is unity but it's written in c#


The object is rotating randomly between the 135 and 225 degrees.

But now I want to add a rule/s.

If for example the object is rotating to the left twice in a row for example if rand is 185 and then time rand is 200 so that's meaning the object rotated or more stayed on the left side area. In this case I want to pick a random number from the right area.

Same if there are two numbers in a row of the right side so the next time should be picked a random number from the left area.

I tried to use a counter and tried to use two counters one for each direction but can't figure out how to do it.

Update is calling each frame nonstop. So if one of the sides picked randomly twice in a row the next time should be picked randomly from the opposite side.


I tried this but it's not working good, I could count more then two times in a row in one of the sides :


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateRandom : MonoBehaviour
{
bool noTarget = true;
Quaternion qTo;
float rotateSpeed = 3.0f;
float timer = 0.0f;
int sameDirectionCounter = 0;
public int maxSameDirection = 2;
bool currentDirection = false; // false = left, true = right, would be more elegant to use an enum
private void Start()
{
qTo = Quaternion.Euler(new Vector3(0.0f, Random.Range(135.0f, 225.0f), 0.0f));
}
private void Update()
{
timer += Time.deltaTime;
if (noTarget == true)
{
if (timer > 2)
{
// to arrive at a range from 90 to 270 (180 degrees) we
// take a random range of 0..90, and add or subtract that from
// 180. This will then range from 90 to 270.

float rand = Random.Range(0.0f, 90.0f);
// now flip a coin for left / right
bool newDirection = Random.Range(0.f, 1.0f) < 0.5f;

// now see if the new direction is the same as the old direction
if (newDirection == currentDirection) {
// if we are here, this is the same direction as last time
// so we need to check how many times this happened already,
// and if it happened too many times, force the other direction
sameDirectionCounter += 1;

if (sameDirectionCounter >= maxSameDirection) {
// if we are here, we need to flip the direction
newDirection = !newDirection; // flip direction
samedirectionCounter = 0; // reset count
}

} else {
samedirectionCounter = 0;
}

durrentDirection = newDirection;

// now calculate the effective turn in degrees
float turnAmount = 180.0f;
if (newDirection) {
// right
turnAmount += rand; // add the random value
} else {
// left
turnamount -= rand; // subtract the random value
}
qTo = Quaternion.Euler(new Vector3(0.0f, rand, 0.0f));

timer = 0.0f;
}
// note that the way you use Slerp here will probably work, but only because you compount two errors :)
// because the Slerp value should run from 0..1 (yours will alway be 1/FPS), and because the starting
// slerp value should be constant (yours is constantly changing because you are changing transform.rotation)
transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
}
}
}


I know it's unity but the language is c# and I c an't figure out how to add this rule.

Continue reading...
 
Back
Top