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 3honi · Apr 10, 2016 at 09:26 AM · c#shootingdirectionbullet

C# 2D Shooting a bullet based on player direction

I'm fairly new to unity so this probably isn't the best way to do this but the bullet goes in the right direction but if the player changes direction, the bullet changes direction, I knew this would happen but I have no solution to fix it. Thanks!

Player Code:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public int direction = 0;
     public float speed = 0;
     public int mana = 0;
     public Bullet myBullet;
     public KeyCode shoot;
     public KeyCode moveUp;
     public KeyCode moveDown;
     public KeyCode moveLeft;
     public KeyCode moveRight;
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         Movement();
         Shoot();
 
     }
     void Shoot () {
         if (Input.GetKey(shoot) && mana > 0)
         {
             Instantiate(myBullet);
             mana -= 1;
         }
     }
     void Movement () {
         if (Input.GetKey(moveUp))
         {
             transform.Translate(Vector2.up * speed);
             direction = 1;
         }
         if (Input.GetKey(moveDown))
         {
             transform.Translate(Vector2.down * speed);
             direction = 2;
         }
         if (Input.GetKey(moveLeft))
         {
             transform.Translate(Vector2.left * speed);
             direction = 3;
         }
         if (Input.GetKey(moveRight))
         {
             transform.Translate(Vector2.right * speed);
             direction = 4;
         }
     }
 }
 


Bullet Code:

 using UnityEngine;
 using System.Collections;
 
 public class Bullet : MonoBehaviour
 {
     public float speed = 1;
     // Use this for initialization
     void Start()
     {
         GameObject player = GameObject.Find("Player");
         Transform playerTransform = player.transform;
         // get player position
         Vector2 position = playerTransform.position;
         transform.position = new Vector2(position.x, position.y);
     }
 
     // Update is called once per frame
     void Update()
     {
         GameObject myPlayer = GameObject.Find("Player");
         Player player = myPlayer.GetComponent<Player>();
         if (player.direction == 1)
         {
             ShootUp();
         }
         else if (player.direction == 2)
         {
             ShootDown();
         }
         else if (player.direction == 3)
         {
             ShootLeft();
         }
         else if (player.direction == 4)
         {
             ShootRight();
         }
     }
     void ShootUp()
     {
         transform.Translate(Vector2.up * speed);
     }
     void ShootDown()
     {
         transform.Translate(Vector2.down * speed);
     }
     void ShootLeft()
     {
         transform.Translate(Vector2.left * speed);
     }
     void ShootRight()
     {
         transform.Translate(Vector2.right * speed);
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DrGEEK · Apr 10, 2016 at 11:58 AM

You just need to store the direction in a variable when you create the bullet, then, move the bullet with the vector in the variable

Bullet code:

  using UnityEngine;
  using System.Collections;
  
  public class Bullet : MonoBehaviour
  {
      public float speed = 1;
      private Vector2;
      // Use this for initialization
      void Start()
      {
          GameObject player = GameObject.Find("Player");
          Transform playerTransform = player.transform;
          // get player position
          Vector2 position = playerTransform.position;
          transform.position = new Vector2(position.x, position.y);
          GameObject myPlayer = GameObject.Find("Player");
          Player player = myPlayer.GetComponent<Player>();
      }
  
      // Update is called once per frame
      void Update()
      {
          //i moved the player vector update to the start function
          if (player.direction == 1)
          {
              ShootUp();
          }
          else if (player.direction == 2)
          {
              ShootDown();
          }
          else if (player.direction == 3)
          {
              ShootLeft();
          }
          else if (player.direction == 4)
          {
              ShootRight();
          }
      }
      void ShootUp()
      {
          transform.Translate(Vector2.up * speed);
      }
      void ShootDown()
      {
          transform.Translate(Vector2.down * speed);
      }
      void ShootLeft()
      {
          transform.Translate(Vector2.left * speed);
      }
      void ShootRight()
      {
          transform.Translate(Vector2.right * speed);
      }
  }

I think the script would be like that

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 3honi · Apr 10, 2016 at 12:05 PM 0
Share

I already tried that and it only moved a little bit, also you can't access the variables in the start function in the update function if they're not public.

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

139 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

Related Questions

Trying to get Player to shoot in both directions depending on what side he is facing? 0 Answers

Make bullet face direction of movement 1 Answer

Unity 2d simple c# shooting script 1 Answer

No overload for method 'fireBullet' takes 0 arguments 1 Answer

Instantiate position for shooting projectile is way off. Could I get some help? 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