Hi everyone!

At the moment me and my buddys are developing a PC game and need some help with the programing.

I have a turret that shoots the player, code works fine but I need help with random rotation.

I want the turret to rotatate random when the player is at a distance.

Like, a little bit to the left… then a little bit to the right, and to the right a little more maybe As if it were looking around for something.

I have done the code so it works all the way exept this part.

So here is my else statement that need the random rotation.

else

```
{
transform.rotation.y += Random.Range(-1,1);
}
```

This ^ did not work, It just rotates once then stops.

Have it in the update()

This is in the logic that i got from your code

```
int rotate = Random.Range(-1,2);
transform.Rotate(0,rotate,0,Space.Self);
```

It will stand still. Rotate to the left or right do a random new number every frame. But it will look rly retarded (atleast what i saw when i tried it out)

I would suggest you to atleast make it rotate in 1 second or more until it changes in a new random.

I think this is something you are looking for. It is not perfect but it is something you could work on

```
using UnityEngine;
using System.Collections;
public class PlayWithRotation : MonoBehaviour {
int _bFlag;
float _currentTime;
float _delay = 1.0f;
void Start ()
{
_currentTime = Time.time;
}
void RotateTurret(int rot)
{
transform.Rotate(0,rot,0,Space.Self);
}
void Update ()
{
if(Time.time > _currentTime)
{
_currentTime += _delay;
//_bFlag will be -1 to 1
_bFlag = Random.Range(-1,2);
}
RotateTurret(_bFlag);
}
}
```

Hope this helps you out!

Here are two more solutions. Attach them to a block in an empty scene to start.

```
#pragma strict
var MinAngle = -70.0;
var MaxAngle = 70.0;
var timestamp : float;
var recalcFreq = 1.0; // Seconds before selecting a new angle
var speed = 35.0;
var qTo : Quaternion;
var randomSeeking = true;
function Update () {
if (randomSeeking) {
if (timestamp < Time.time) {
timestamp = timestamp + recalcFreq;
qTo = Quaternion.Euler(0.0, Random.Range(MinAngle, MaxAngle), 0.0);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * speed);
}
}
```

This second one produces a smoother seeking:

```
#pragma strict
var neutralAngle = 0.0; // Angle that is midway between the min and max angle.
var angleRange = 70.0; // Delta (both sides) of the neutral Angle.
var trigSpeed1 = 0.2;
var trigSpeed2 = 0.5;
var qTo : Quaternion;
var speed = 20.0; // Speed of the rotation.
var randomSeeking = true;
function Update () {
if (randomSeeking) {
var angle = neutralAngle + (Mathf.Sin(trigSpeed1 * Time.time) + Mathf.Sin(trigSpeed2 * Time.time)) / 2.0 * angleRange;
qTo = Quaternion.Euler(0.0, angle, 0.0);
}
//transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * speed);
transform.rotation = Quaternion.Lerp(transform.rotation, qTo, Time.deltaTime * speed);
}
```