Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gringofx · May 05, 2012 at 03:45 PM · gameobjectenemybulletrandomize

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) { }

} }

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sebas · May 05, 2012 at 04:12 PM 0
Share

Randomize as in scatter? Something like this? http://answers.unity3d.com/questions/11696/how-to-make-my-bullets-spread-when-fired.html

avatar image gringofx · May 06, 2012 at 02:42 AM 0
Share

i would like to randomize the speed.

avatar image DracoScorpius · May 06, 2012 at 04:35 AM 0
Share

Where your speed is, I'd use a range, like from a certain speed to another certain speed.

avatar image Lo0NuhtiK · May 06, 2012 at 06:49 AM 0
Share

"Randomizate" makes me think of the guy in Fable II with the 'Thessaruss' and all of his weird words he usificated.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Doireth · Jul 22, 2012 at 10:57 AM 0
Share

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.

avatar image
0

Answer by gringofx · May 06, 2012 at 12:25 PM

Thanks Alot, works PERFECT!!!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · May 06, 2012 at 01:55 PM 1
Share

This is supposed to be a comment. You shouldn't make this as answer. Also, you should mark it as answered.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges