• 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 unity_En9Jz88Uh8XLKA · Apr 13, 2021 at 07:38 PM · scripting problem2d gamescripting beginnergameobjectsscripterror

Bullets follow the ship in 2D Space Shooter game

Hello, i have a bullet where when i shoot a bullet and move the ship the bullet shootsforwards but follows on the ship Position to left an right.

Here my scripts:

public class Bullet : MonoBehaviour { public Rigidbody2D Rigidbody; public float Speed; public int Damage; public GameObject ExplosionPrefab;

 private void Start()
 {
     Rigidbody.GetComponent<Rigidbody2D>();
 }

 private void FixedUpdate()
 {
     Rigidbody.AddRelativeForce(Vector2.up * Speed);
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.CompareTag("Enemy"))
     {
         var meteoroidController = collision.GetComponent<MeteoridController>();

         meteoroidController.TakeDamage(Damage);

         Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);

         Destroy(gameObject);
     }
 }

}

public class WeaponSystem : MonoBehaviour { public Transform[] Spawnpoints; public Bullet BulletPrefab; public float FireRate = 1; public AudioSource SoundEffect;

 private float _fireRatecounter;

 private void Update()
 {
     _fireRatecounter += Time.deltaTime;
 }

 public void Fire()
 {
     if (_fireRatecounter >= FireRate)
     {
         _fireRatecounter = 0;

         SoundEffect.Play();

         foreach (var spawnPoint in Spawnpoints)
         {
             Instantiate(BulletPrefab, spawnPoint);
         }
     }
 }

} thanks for the help

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ermiq · Apr 15, 2021 at 02:30 PM

So, what you do with the bullets is just this:

 foreach (Transform spawnPoint in Spawnpoints)
 {
     Instantiate(BulletPrefab, spawnPoint);
 }

In human language: take all the transform objects from the Spawnpoints list (seems like you have two of them, to the left/right of the ship, and they are attached to the ship probably), create a bullet object and attach it to the spawnpoint object.
Basically it is the same as if you just drag&dropped a bullet onto the spawnpoint in the scene editor. Obviously the bullets will just hang at the given position as children/attachments of the spawnpoint objects and won't fly anywhere.

What you need is to instantiate the bullets without attaching them to anything so they would live on their own as independent objects and then move them to the desired direction.

Remove the parenting argument from the Instantiate (...) method call. It should be just

 Instantiate(BulletPrefab);

or some other overload of the method that doesn't set the newly created object as a child of another object.

To move the bullets you could either use physics engine and apply some force to the bullets rigidbody component or manipulate the bullets positions directly by setting their transform.position property in Update().

There're tons of tutorials and answers available on YouTube, UnityLearn, StackExchange, etc. on how to do that.

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 UnityM0nk3y · Apr 14, 2021 at 11:09 AM

Perhaps try:

  Rigidbody.AddForce(Vector2.up * 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 unity_En9Jz88Uh8XLKA · Apr 14, 2021 at 12:51 PM 0
Share

didn't work

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

253 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 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 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 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 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 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 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 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

Why wont my character stop moving when it collides with the obstacles?,Why doesn't my movement script stop? 0 Answers

Why does my player not move? 1 Answer

Linking Image to item list 1 Answer

Add more seconds to the timer when colliding with an object. 2 Answers

(C#) SetActive isn't working on my UI 2 Answers


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