Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by B00657332 · Oct 10, 2015 at 01:19 PM · c#bulletshootfirefire rate

Constant fire at target

I need constant fire from a 'turret' to shoot at the player. Right now it's not shooting at all. Where am I going wrong? The code for the bullets is below..

 using UnityEngine;
 using System.Collections;
 
 public class BulletScript : MonoBehaviour
 {
     public AudioClip collisionPop;
     public float bulletSpeed = 15f;
     public float bulletLife = 2f;
     Rigidbody2D rb2D;
     float spawnTime;
 
     bool bulletActive = false;
 
     public GameObject particle;
     public Transform splatter;
     public int numberParticles = 10;
 
 
 
 
     // Use this for initialization
     void Start()
     {
         spawnTime = Time.time;
         rb2D = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Time.time > spawnTime + bulletLife) Destroy(gameObject);
     }
 
     // Think this is where I'm going wrong?
     void FixedUpdate()
     {
         Vector2 currentDirection = new Vector2((float)Mathf.Cos(rb2D.rotation * Mathf.Deg2Rad), (float)Mathf.Sin(rb2D.rotation * Mathf.Deg2Rad));
 
         rb2D.MovePosition(rb2D.position + currentDirection * bulletSpeed * Time.deltaTime);
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
             bulletActive = true;
             if (other.gameObject.tag == "Player")
         {
             GetComponent<AudioSource>().PlayOneShot(collisionPop);
             GetComponentInChildren<SpriteRenderer>().enabled = false;
             Destroy(gameObject, collisionPop.length);               // destroy after audio finishes
 
             for (int i = 0; i < numberParticles; i++)
             {
                 splatter.rotation = Random.rotationUniform;
                 Instantiate(particle, transform.position, splatter.rotation);
             }
         }
     }
 
 }

Comment
Add comment · Show 1
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 Chris-Paul · Oct 10, 2015 at 01:55 PM 0
Share

So you are probably instantiating bullets, with this script on them, the problem is that they don't move or what? what would be the trajectory? straight ? or do you want them to follow the player?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by B00657332 · Oct 10, 2015 at 02:56 PM

@Chris When I run the game, no bullets are firing at the player at all. I have the turrets following the position of the player, but there are no bullets coming out of them :/

Comment
Add comment · 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
0

Answer by Chris-Paul · Oct 10, 2015 at 03:29 PM

You will need a script that handles:

  • The creation / instantiation of the bullets

  • Place the bullet at the turret's barrel position

  • Rotate the bullet to the barrel's rotation

  • Set it's velocity to a high value, so the bullet goes fast

  • Set the rigidbody's property : "UseGravity" to false, so it doesn't fall.

  • For that you will need an empty game object that will represent the barrel's position and orientation.

After that, in your Bullet script, you delete the FixedUpdate() method, as you won't need to move the bullet manually, it's going to move alone, here it's an example of such a script:

 using UnityEngine;
 using System.Collections;
 
 public class FireBullets : MonoBehaviour {
 
     [SerializeField] float roundsPerMinute = 500;    // Set the fire rate to 500 bullets per minute.
     [SerializeField] float bulletSpeed = 100;
     [SerializeField] Transform barrel;                // Assign the barrel position in inspector.
     [SerializeField] Rigidbody2D bulletPrefab;        // Assign a bullet prefab here, 
                                                     // it must have a Rigidbody component attached
 
     float timeBetweenShots;
     float nextTimeCanShoot;
     
     
     void Start() {
         // Get the time between shots in seconds.
         timeBetweenShots = 60.0f / roundsPerMinute;
     }
     
     void Update() {
         // If the player holds the left mouse button.
         if(Input.GetMouseButton(0)) {
             // We try to shoot.
             TryShoot();
         }
     }
     
     void TryShoot() {
         // If not enough time has passed between shots, stop the function.
         if(Time.time < nextTimeCanShoot) {
             return;
         }
         
         // Create the bullet at the barrel's position and rotation.
         Rigidbody2D bullet = Instantiate(bulletPrefab, barrel.position, barrel.rotation) as Rigidbody2D;
         bullet.gravityScale = 0;
         bullet.velocity = new Vector2(barrel.forward.x, barrel.forward.y) * bulletSpeed;
     }
 }
 






Comment
Add comment · 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

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

32 People are following this question.

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

Related Questions

NEED HELP PLEASE! Unexpected symbol. 0 Answers

My bullet won't go forward? Please check my script and advise me 2 Answers

C# 2D Shooting a bullet based on player direction 1 Answer

how to activate a particle when bullet hits a collider? 1 Answer

Bullet spawning at high Player speed 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