- Home /
Randomizate bullets help
How can i randomizate the bullets in this script ?
using UnityEngine; using System.Collections;
public class EnemyAAi : MonoBehaviour { public Transform target; private float speed = 0; public float normalSpeed = 0; public float maxSpeed = 0; public float rotateSpeed = 0;
public float lookAtDistance = 80;
public float attackRange = 40;
private Transform myTransform;
private bool moveForward = false;
public Rigidbody bullet;
public float bulletSpeed = 0;
public AudioClip laserSound;
public Transform muzzle;
public float attackTimer = 1;
private float timer = 0;
// Use this for initialization
void Start ()
{
myTransform = transform;
}
// Update is called once per frame
void Update ()
{
//40 to attack
//80 to look
float distance = Vector3.Distance(target.position, myTransform.position);
if(distance < lookAtDistance)
{
LookAtTarget();
speed = maxSpeed;
moveForward = true;
if(distance <= attackRange)
{
moveForward = false;
//attack code
TimeStuff();
}
}
else
{
moveForward = false;
}
//this is for making shure the ship moves and stops
if(moveForward)
{
MoveToTarget();
}
else
{
return;
}
}
//move towards player
private void MoveToTarget()
{
rigidbody.AddForce(myTransform.forward * speed);
}
//look at player
private void LookAtTarget()
{
//myTransform.LookAt(target);
Quaternion rotation = Quaternion.LookRotation(target.position - myTransform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotation, rotateSpeed * Time.deltaTime);
}
private void ShootBullet()
{
Rigidbody clone;
clone = Instantiate(bullet, muzzle.position,muzzle.rotation) as Rigidbody;
clone.velocity = transform.forward * 100;
clone.transform.parent = transform;
timer = attackTimer;
}
private void TimeStuff()
{
//making shure timer is doing what is supposed to do
if(timer <= 0)
{
timer = 0;
}
if(timer > 0)
{
timer -= Time.deltaTime;
}
if(timer == 0)
{
ShootBullet();
}
}
public float health=100; //the health variable. public float damage=10; //damage amount. public void hit() { health-= damage; if (health < 0) { }
} }
Randomize as in scatter? Something like this? http://answers.unity3d.com/questions/11696/how-to-make-my-bullets-spread-when-fired.html
Where your speed is, I'd use a range, like from a certain speed to another certain speed.
"Randomizate" makes me think of the guy in Fable II with the 'Thessaruss' and all of his weird words he usificated.
Answer by fafase · May 06, 2012 at 06:43 AM
You say you want to randomize the speed:
private void ShootBullet()
{
Rigidbody clone;
clone = Instantiate(bullet, muzzle.position,muzzle.rotation) as Rigidbody;
float speed = Random.Range(minSpeed, maxSpeed); // HERE
clone.velocity = transform.forward * speed;
clone.transform.parent = transform;
timer = attackTimer;
}
Is that what you meant? Now each time you instatiate a bullet a random value between the two given will be used for speed.
Small, unrelated point but you don't need to declare a variable inside a function as private (or any other accessor) as variables declared inside a function always have local scope so it's already implied.
Answer by gringofx · May 06, 2012 at 12:25 PM
Thanks Alot, works PERFECT!!!
This is supposed to be a comment. You shouldn't make this as answer. Also, you should mark it as answered.
Your answer
Follow this Question
Related Questions
Script Not counting enemy kills. 1 Answer
How can I randomize the speed of enemy movement? 2 Answers
How to destroy GameObject's script(s)? 3 Answers
get closest enemy 1 Answer
Destroy object when not looking 1 Answer