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 The_Catalyst · Dec 20, 2017 at 07:28 AM · 2d gameshooterbulletstop-down

How to avoid Bullets to Change speed while flying,How to avoid Bullets speed changing while the Bullet is flying

I wanted to make a Top Down 2D Shooter and I am currently working on the Shooting. The Problem i am facing is that after Shooting a bullet and the next one should have a diferent direction. The direction of both bullets gets set to the same. So after Shooting some bullets they all have the same direction depending on where i move. So how do i make sure that only 1 Bullet gets the Speed vector at a Time?

Here is my Code:

public class Player : MonoBehaviour { public GameObject Bullet;

 [SerializeField]
 Sprite []Guns;

 [SerializeField]
 Rigidbody2D rb;

 [SerializeField]
 Animator Anim;
 
 [SerializeField]
 SpriteRenderer Gun;

 [SerializeField]
 Transform FirePos;
     
 
 public Vector2 LastInputVector;
 
 public VirtualJoystick JoystickMove;

 public float speed;    
 Vector2 temp;

 void FixedUpdate()
 {
     PlayerMove();        
 }

 public void OnButtonPress()
 {       
         Instantiate(Bullet, FirePos.position, Quaternion.identity);
     
 }


 void PlayerMove()
 {
     

     float VertMove = JoystickMove.Vertical();
     float HoriMove = JoystickMove.Horizontal();
     float VertSpeed = speed * VertMove;
     float HoriSpeed = speed * HoriMove;
     float bound = 0.4f;
     

     if (HoriMove > bound)
     {
         if (VertMove > bound)
         {
             //UpR               
             temp = new Vector2(HoriSpeed, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", true);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[1];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(.3f, 0.15f, 0);
             this.transform.localScale = new Vector3(5, 5, 0);
             

         }
         else if (VertMove < -bound)
         {
             //DownR
             temp = new Vector2(HoriSpeed, VertSpeed);
             Anim.SetBool("IsDiagDownR", true);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[0];
             Gun.sortingOrder = 2;
             Gun.transform.position = transform.position + new Vector3(.2f, -0.3f, 0);
             this.transform.localScale = new Vector3(5, 5, 0);
             
         }
         else
         {
             //Right
             temp = new Vector2(HoriSpeed, 0);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", true);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[3];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(.4f, -0.15f, 0);
             this.transform.localScale = new Vector3(5, 5, 0);
             
         }

     }
     else if (HoriMove < -bound)
     {

         if (VertMove > bound)
         {
             //UpL
             temp = new Vector2(HoriSpeed, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", true);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[1];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(-.3f, 0.15f, 0);
             this.transform.localScale = new Vector3(-5, 5, 0);
             
         }
         else if (VertMove < -bound)
         {
             //DownL
             temp = new Vector2(HoriSpeed, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", true);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[0];
             Gun.sortingOrder = 2;
             Gun.transform.position = transform.position + new Vector3(-.2f, -0.3f, 0);
             this.transform.localScale = new Vector3(-5, 5, 0);
             
         }
         else
         {
             //Left
             temp = new Vector2(HoriSpeed, 0);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", true);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[3];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(-.4f, -0.15f, 0);
             this.transform.localScale = new Vector3(-5, 5, 0);
             
         }
     }
     else
     {
         if (VertMove > 0)
         {
             //Up
             temp = new Vector2(0, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", true);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[4];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(0, .5f, 0);
             
         }
         else if (VertMove < 0)
         {
             //Down
             temp = new Vector2(0, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", true);
             Gun.sprite = Guns[2];
             Gun.sortingOrder = 2;
             Gun.transform.position = transform.position + new Vector3(0, -.4f, 0);
             
         }
         else
         {
             temp = Vector2.zero;      
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
         }
     }
     
     rb.velocity = temp;
     LastInputVector = temp;
 }
 public Vector2 GetInputVector()
 {
     
     return LastInputVector;
 }
 }


public class BulletScript : MonoBehaviour {

 public Rigidbody2D BulletRb;
 public Player player;
 

 public void Awake()
 {
     BulletRb = GetComponent<Rigidbody2D>();
     player = FindObjectOfType<Player>();
     
 }

 public void Update()
 {
     ShootBullet();
 }
 
 public void ShootBullet()
 {
         Vector2 dir = player.GetInputVector();
         if (dir.x > 0)
         {
             if (dir.y > 0)
             {
             
                 BulletRb.velocity = new Vector2(1, 1);
                 //transform.Translate(new Vector2(1, 1) * 17 * Time.deltaTime);

             }
             else if (dir.y < 0)
             {
                 BulletRb.velocity = new Vector2(1, -1);
                 //  transform.Translate(new Vector2(1, -1) * 17 * Time.deltaTime);

             }
             else
             {
                 BulletRb.velocity = new Vector2(1, 0);
                 //  transform.Translate(new Vector2(1, 0) * 17 * Time.deltaTime);
             }

         }
         else if (dir.x < 0)
         {
             if (dir.y > 0)
             {
                 BulletRb.velocity = new Vector2(-1, 1);
                 //  transform.Translate(new Vector2(-1, 1) * 17 * Time.deltaTime);
             }
             else if (dir.y < 0)
             {
                 BulletRb.velocity = new Vector2(-1, -1);
                 // transform.Translate(new Vector2(-1, -1) * 17 * Time.deltaTime);
             }
             else
             {
                 BulletRb.velocity = new Vector2(-1, 0);
                 // transform.Translate(new Vector2(-1,0) * 17 * Time.deltaTime);
             }
         }
         else
         {
             if (dir.y > 0)
             {
                 BulletRb.velocity = new Vector2(0, 1);
                 // transform.Translate(new Vector2(0, 1) * 17 * Time.deltaTime);
             }
             else if (dir.y < 0)
             {
                 BulletRb.velocity = new Vector2(0, -1);
                 // transform.Translate(new Vector2(0, -1) * 17 * Time.deltaTime);
             }

         }
     Debug.Log(dir);       
   }

}

,I wanted to make this Top Down 2D shooter and am currently working on the Shooting. The Problem i am facing is that the Bullets when they are being shot are getting their velocity vector changed all at once. I tried really hard to make it, so that only 1 Bullet at a Time gets their Speed, but it didnt work. I am still learning unity though.

Here is my Code:

public class Player : MonoBehaviour { public GameObject Bullet;

 [SerializeField]
 Sprite []Guns;

 [SerializeField]
 Rigidbody2D rb;

 [SerializeField]
 Animator Anim;
 
 [SerializeField]
 SpriteRenderer Gun;

 [SerializeField]
 Transform FirePos;
     
 
 public Vector2 LastInputVector;
 
 public VirtualJoystick JoystickMove;

 public float speed;    
 Vector2 temp;

 void FixedUpdate()
 {
     PlayerMove();        
 }

 public void OnButtonPress()
 {       
         Instantiate(Bullet, FirePos.position, Quaternion.identity);
     
 }


 void PlayerMove()
 {
     

     float VertMove = JoystickMove.Vertical();
     float HoriMove = JoystickMove.Horizontal();
     float VertSpeed = speed * VertMove;
     float HoriSpeed = speed * HoriMove;
     float bound = 0.4f;
     

     if (HoriMove > bound)
     {
         if (VertMove > bound)
         {
             //UpR               
             temp = new Vector2(HoriSpeed, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", true);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[1];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(.3f, 0.15f, 0);
             this.transform.localScale = new Vector3(5, 5, 0);
             

         }
         else if (VertMove < -bound)
         {
             //DownR
             temp = new Vector2(HoriSpeed, VertSpeed);
             Anim.SetBool("IsDiagDownR", true);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[0];
             Gun.sortingOrder = 2;
             Gun.transform.position = transform.position + new Vector3(.2f, -0.3f, 0);
             this.transform.localScale = new Vector3(5, 5, 0);
             
         }
         else
         {
             //Right
             temp = new Vector2(HoriSpeed, 0);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", true);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[3];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(.4f, -0.15f, 0);
             this.transform.localScale = new Vector3(5, 5, 0);
             
         }

     }
     else if (HoriMove < -bound)
     {

         if (VertMove > bound)
         {
             //UpL
             temp = new Vector2(HoriSpeed, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", true);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[1];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(-.3f, 0.15f, 0);
             this.transform.localScale = new Vector3(-5, 5, 0);
             
         }
         else if (VertMove < -bound)
         {
             //DownL
             temp = new Vector2(HoriSpeed, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", true);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[0];
             Gun.sortingOrder = 2;
             Gun.transform.position = transform.position + new Vector3(-.2f, -0.3f, 0);
             this.transform.localScale = new Vector3(-5, 5, 0);
             
         }
         else
         {
             //Left
             temp = new Vector2(HoriSpeed, 0);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", true);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[3];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(-.4f, -0.15f, 0);
             this.transform.localScale = new Vector3(-5, 5, 0);
             
         }
     }
     else
     {
         if (VertMove > 0)
         {
             //Up
             temp = new Vector2(0, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", true);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
             Gun.sprite = Guns[4];
             Gun.sortingOrder = 0;
             Gun.transform.position = transform.position + new Vector3(0, .5f, 0);
             
         }
         else if (VertMove < 0)
         {
             //Down
             temp = new Vector2(0, VertSpeed);
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", true);
             Gun.sprite = Guns[2];
             Gun.sortingOrder = 2;
             Gun.transform.position = transform.position + new Vector3(0, -.4f, 0);
             
         }
         else
         {
             temp = Vector2.zero;      
             Anim.SetBool("IsDiagDownR", false);
             Anim.SetBool("IsDiagDownL", false);
             Anim.SetBool("IsDiagUpR", false);
             Anim.SetBool("IsDiagUpL", false);
             Anim.SetBool("IsUp", false);
             Anim.SetBool("IsSide", false);
             Anim.SetBool("IsDown", false);
         }
     }        
     rb.velocity = temp;
     LastInputVector = temp;
 }
 public Vector2 GetInputVector()
 {        
     return LastInputVector;
 }




public class BulletScript : MonoBehaviour {

 public Rigidbody2D BulletRb;
 public Player player;
 

 public void Awake()
 {
     BulletRb = GetComponent<Rigidbody2D>();
     player = FindObjectOfType<Player>();
     
 }

 public void Update()
 {
     ShootBullet();
 }
 
 public void ShootBullet()
 {
         Vector2 dir = player.GetInputVector();
         if (dir.x > 0)
         {
             if (dir.y > 0)
             {
             
                 BulletRb.velocity = new Vector2(1, 1);
                 //transform.Translate(new Vector2(1, 1) * 17 * Time.deltaTime);

             }
             else if (dir.y < 0)
             {
                 BulletRb.velocity = new Vector2(1, -1);
                 //  transform.Translate(new Vector2(1, -1) * 17 * Time.deltaTime);

             }
             else
             {
                 BulletRb.velocity = new Vector2(1, 0);
                 //  transform.Translate(new Vector2(1, 0) * 17 * Time.deltaTime);
             }

         }
         else if (dir.x < 0)
         {
             if (dir.y > 0)
             {
                 BulletRb.velocity = new Vector2(-1, 1);
                 //  transform.Translate(new Vector2(-1, 1) * 17 * Time.deltaTime);
             }
             else if (dir.y < 0)
             {
                 BulletRb.velocity = new Vector2(-1, -1);
                 // transform.Translate(new Vector2(-1, -1) * 17 * Time.deltaTime);
             }
             else
             {
                 BulletRb.velocity = new Vector2(-1, 0);
                 // transform.Translate(new Vector2(-1,0) * 17 * Time.deltaTime);
             }
         }
         else
         {
             if (dir.y > 0)
             {
                 BulletRb.velocity = new Vector2(0, 1);
                 // transform.Translate(new Vector2(0, 1) * 17 * Time.deltaTime);
             }
             else if (dir.y < 0)
             {
                 BulletRb.velocity = new Vector2(0, -1);
                 // transform.Translate(new Vector2(0, -1) * 17 * Time.deltaTime);
             }

         }
     Debug.Log(dir);   
 }

}

Thank you for your 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

0 Replies

· Add your reply
  • Sort: 

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

127 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

Related Questions

How to give the player a limited amount of moves? 1 Answer

How to prevent objects to push in 2d top-down? 0 Answers

Shoot at mouse cursor 0 Answers

[2D] Make player push object 1 Answer

Weapon following mouse and orbiting Player 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