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 /
This question was closed Feb 18, 2016 at 03:34 AM by PersimmonSquares for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by PersimmonSquares · Jan 27, 2016 at 03:04 PM · enemyprojectileprojectiles

Enemy projectile direction setting

Hey everyone. I've run into an issue with using "Turret-type" enemies in a simple 2D game, programmed to fire a projectile based on the direction and rotation it's currently in. The problem is that when I have two facing different directions, their projectiles get 'confused' as to which way to go, is the best way to put it. I have tested it with just one enemy and it works fine. Is there a better way to make sure that the bullet 'knows' specifically which direction it was fired from? Thanks in advance. For reference, here's the code for the bullet in question I'm using:

     public class EBullet1Behavior : MonoBehaviour {
     
         private Rigidbody2D bulletRigi;
         public Playermovement playerMovement;
         public EnemyType2Properties enemyType2;
         public FindVector findVector;
     
         public Transform bulletPoint;
     
         public float speed;
         public float destroyTime;
     
         // Use this for initialization
         void Start () {
     
             playerMovement = FindObjectOfType<Playermovement> ();
         enemyType2 = FindObjectOfType<EnemyType2Properties> ();
             findVector = FindObjectOfType<FindVector>();
         bulletRigi = GetComponentInParent<Rigidbody2D> ();
     
     
                 bulletRigi.velocity = new Vector2 (findVector.x * speed, findVector.y * speed);
             transform.right = new Vector3(findVector.x, findVector.y, 0);
     
     
     
         }
         
         // Update is called once per frame
         void Update () {
             Destroy (gameObject, destroyTime);
         }
     }
     
 

While the firing bit is fairly simple:

     IEnumerator Firing() {
         if (stunned == false && canFire == true)
         {
             Instantiate(EnemyBullet, bulletPoint.position, bulletPoint.rotation);
             yield return new WaitForSeconds(fireSpeed);
             Debug.Log("Fire!");
             canFire = true;
  // Firing capability is turned 'off' elsewhere in the script
             Update();
         }
     }

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 Teravisor · Jan 27, 2016 at 04:21 PM 0
Share
 GameObject bullet = Instantiate (....);
 bullet.GetComponent<EBullet1Behavior>().findVector = something;

like that...

1 Reply

  • Sort: 
avatar image
0

Answer by Baste · Jan 27, 2016 at 04:43 PM

The bullet has a ton of properties it doesn't need to have. It's also getting it's direction by finding a FindVector object, and grabbing information from that.

The FindObjectOfType method finds an object of that type. Which object it finds is not guaranteed, but it's usually the same one for every call on the same frame, so both your guns' bullets are finding the same FindVector object.

The solution here is to not use the convoluted FindVector method, and skip the start method. Instead, give the bullet a Fire method that takes the direction. So along the lines of:

 //bulletscript
 public class BulletScript : MonoBehaviour {

     public float speed; //set in prefab
     public float destroyAfter; // set in prefab

     public void Fire() {
         GetComponent<RigidBody>().velocity = transform.forward * force;
         Destroy(gameObject, destroyAfter);
     } 
 }

That's really all you need. Your firing code will simply be:

 GameObject bulletObj = Instantiate(EnemyBullet, bulletPoint.position, bulletPoint.rotation);
 bulletObj.Fire();

The Instantiate call sets the rotation of the bullet. This means that if the Fire-code sets the velocity straight forward (transform.forward), it should go straight ahead.

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 PersimmonSquares · Jan 27, 2016 at 10:06 PM 0
Share

Hey again. I should probably have specified that I'm working with Unity2D, so I made the simple adjustment of setting the transform.forward to transform.right. Unfortunately, now all my bullets are moving rightwards regardless of which way the enemy is facing. I'll try and work on this myself, but any pointers would be appreciated.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Weapon Rotation Behaviour Changes on Flipping Sprite? 1 Answer

How do I get the enemy's to rotate to players and shoot at them? 0 Answers

Bullet goes sideways when the player gets hit 1 Answer

How to make my snowball model to actually shoot like a projectile? 0 Answers

2.5D Projectile Help 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