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 Heretsu · Sep 13, 2015 at 10:21 AM · c#platformershoot

Problem: Shoot bullet in direction of player C#

I'm doing a 2D platformer game where the enemy should shoot to the player. My problem is, that the bullets always fly in the right direction, even if the enemy is looking to the left side(if the player is at the right side of the enemy, the shooting works perfect). The bullet spawn at the left side, but tries to change his direction to the right. Example: at the blue points the bullets spawn, but like you can see in the picture below, the bullets fly to the right direction. alt text Script for the direction:

 public class EnemyShoot : MonoBehaviour {
 
     float speed;
     Vector2 myDirection;
     bool isReady;
 
     void Awake(){
         speed = 5f;
         isReady = false;
     }
     
     void Start () {
     
     }
 
     public void SetDirection(Vector2 direction) {
         myDirection = direction.normalized;
         isReady = true;
     }
 
     void Update () {
     
         if (isReady) {
             Vector2 position = transform.position;
             position += myDirection * speed * Time.deltaTime;
             transform.position = position;
         }
     }
 }

Method, where the bullet get shot:

 void FireEnemyBullet()
     {
         GameObject player = GameObject.Find ("Player");
         if(player != null)
         {
             
             GameObject bullet = (GameObject)Instantiate(waterball);
             bullet.transform.position = transform.position;
             
             Vector2 direction = player.transform.position - bullet.transform.position;
             bullet.GetComponent<EnemyShoot>().SetDirection(direction);
         }
         
     }

unbenannt.png (91.7 kB)
Comment
Add comment · Show 6
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 pako · Sep 13, 2015 at 01:25 PM 0
Share

@Serdan 's comment right. player-bullet will give you the correct direction (+1)

avatar image Serdan pako · Sep 13, 2015 at 01:56 PM 0
Share

Read the question again. It's about the enemy shooting the player.

Heretsu gets a direction vector by subtracting bullet position from player position. There's nothing wrong with that. In fact, I can't see anything wrong with the code as posted and certainly nothing that would explain the curved path.

@Heretsu, try standard debugging procedure: Simplify until it does what you would expect.

(Btw, GameObject.Find() is expensive, you should use a permanent reference.)

avatar image pako pako · Sep 13, 2015 at 03:10 PM 0
Share

@Serdan you are right, I misread the question, so my answer is not relevant and I'll just edit it and convert it to a comment.

avatar image maccabbe · Sep 13, 2015 at 01:51 PM 0
Share

Edit: Serdan is right, the direction should be player-bullet.

What values are you using to deter$$anonymous$$e direction? For instance, what do you get when you add the following to line 9 of FireEnemyBullet?

       Debug.Log("Player: "+player.transform.position+" Bullet: "+bullet.transform.position);
avatar image Serdan · Sep 13, 2015 at 02:02 PM 2
Share

a-b will get you a direction vector from b to a.

So player-bullet will get you a direction from bullet to player, which is what you want in this scenario.

avatar image sodrix · Sep 13, 2015 at 02:06 PM 0
Share

You are using Global coordinates or Local coordinates?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Heretsu · Sep 14, 2015 at 11:03 AM

Thanks for your answers, I fixed the problem. I dont know how exactly, but I played arround with the scripts and it worked. At first I changed myDirection = direction.normalized; to myDirection = direction;

and it worked for me, but if I change the code back to the origin, it still works like it should. Maybe the script had some problem with a other script that was attached to the object.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Movement Controls 1 Answer

How to Change Rigidbody Type OnCollision/Trigger with Script? 1 Answer

How To Make My Player Only Move on the ground? 1 Answer

How to reset the rotation when respawning? 1 Answer

How to tidy up my code for Unity 5 Basic Platformer 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