Rotate a sprite randomly between 2 points.

Hi!

I’m trying to create enemy holding a flashlight and then rotating it randomly. I got to variables, minRot and maxRot which is the points I wanna rotate around. Here is my current code which kind of works but when it picks a rotation it apperes to pick a rotation on the whole 360 axis, not between the points I have specified.

using UnityEngine;
using System.Collections;

public class AIFlashLight : MonoBehaviour {

	public float rotSpeed;
	public float minRot;
	public float maxRot;

	public Quaternion targetRot;


	void Start () {
		SelectNewTargetRotation ();
	}

	void Update () {
		transform.rotation = Quaternion.RotateTowards (transform.rotation, targetRot, rotSpeed * Time.deltaTime);

		if(transform.rotation == targetRot) {
			SelectNewTargetRotation();
		}
	}

	void SelectNewTargetRotation () {
		targetRot = new Quaternion (0, 0, Random.Range (minRot, maxRot), 1.0f);
	}
}

Thanks for your help
//Proximal Pyro

The issue is with this line of yours:

targetRot = new Quaternion (0, 0, Random.Range (minRot, maxRot), 1.0f);

Looks like you want to select a rotation around z-axis. But you are trying to set rotation around z-axis using Quaternion. The thing is Quaternion are different than Euler Angles and you are trying to set Quaternion similar to Euler Angles.

What you might be looking for is:

targetRot = Quaternion.Euler(0, 0, Random.Range (minRot, maxRot));