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 Denzel_38 · Apr 07, 2015 at 04:31 AM · 2dshootingdirectionplatformerfire

Fire bullet into the direction my player is facing 2D

I am making a 2d megaman like game and I am struggling with making my bullet fire in the direction my player is facing. I am using vector2.right so obviously the bullet will move towards the right, always. But is there a way so that I can make it move to the left when my player is facing the left (same goes for when it's facing right I want both to work)

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 Cachimery · May 01, 2016 at 07:56 AM 0
Share

Hi, I'm Having the same problem, right now my script does the bullet move in both directions but change the direction when my player turn to right or left.

this is the script of the bullet

 public float bulletSpeed;
     private Transform myTransform;
     public GameObject Player;
 
 
     // Use this for initialization
     void Start () {
 
         Player = GameObject.FindGameObjectWithTag ("Player");
 
     myTransform = transform;
 
     }
 
     // Update is called once per frame
     void Update () {
 
         float move = Player.transform.localScale.x;
         float Speed = bulletSpeed * Time.deltaTime;
         if (move > 0){
             myTransform.Translate (Vector3.right * Speed);
             }
         if (move < 0) {
             myTransform.Translate (Vector3.left * Speed);
         }
         if (myTransform.position.x > 10f) {
             Destroy (gameObject);
 
             }
         }
 
 }

and this is the script for the player

 if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.LeftControl)) {
                 Vector3 right = new Vector3 (transform.position.x + (transform.localScale.y / 2), transform.position.y + (transform.localScale.y / 7));
             Instantiate (BulletPrefab, right, transform.rotation);
                 anim.SetTrigger ("Shooting");
             if (StopShooting) {
                 anim.SetBool ("Stop", true);
                 audio.Play ();

I'm new in this world of program$$anonymous$$g so don't be mad if my question is stupid... forgive my english, i'm still learning.

Thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Apr 07, 2015 at 04:37 AM

It depends on how you are modeling the movement. Since you said 2D, are you simply changing which sprite is showing based on the current direction the player is turned toward? If so, you should be able to just use that direction to use vector2.forward, -vector2.forward, vector2.right, and -vector2.right. If you're actually rotating the player's transform, you can use the player's transform.forward vector which will always point in the forward direction of the player in world coordinates.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2d fire from player towards mouse 1 Answer

2D Shooting right only. Doesn't flip direction with character 1 Answer

Instantiate after setting values? 0 Answers

How to destroy only one GameObject??? 0 Answers

My Unity 2D platoformer shooter question. 0 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