- Home /
Need Help with random rotatition on game turret
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()
'transform.rotation is a Quaternion...a non-intuitive, 4D construct. Unless you fully understand the math behind them, you don't want to address the individual x,y,z,w components individually. You can use transform.eulerAngles, but you should never assign individual axes, nor should you depend on any specific eular angle representation. For example you could assign (180,0,0) and immediately read it back and get (0,180,180) which is the same 'physical' rotation in a different euler representation. The easiest way to deal with this issue is to treat eulerAngles as 'write-only'. That is keep your own Vector3. When you want to change an angle, change it in the Vector3 and then assign the Vector3. If you need to read an angle, read it from your own Vector3.
Answer by Brahim113 · May 11, 2013 at 09:05 PM
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!
Did not understand any of those solutions :/ Im a noob programmer and we are under a thight deadline, so this became my sulotion:
transform.Rotate(Time.deltaTime * 6, 0, 0, Space.World);
Now it only rotates one way as idle animation, but that will do i guess. And I dont really understand most of the stuff u guys mentioned about eluer.angels, vector 3 and Quaternion. So *I guess I have to read up on it a little, when i get time :)
thanks
Answer by robertbu · May 15, 2013 at 07:52 PM
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);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rotate Turret to Tag 1 Answer
How To make game installer ? 3 Answers
Beginner need some help on Card Game 2 Answers