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 /
avatar image
0
Question by vbjack72 · Nov 23, 2019 at 12:33 AM · rotationmovementinputspritefire

Shooting in direction of the character movement

Hello,

I am fairly new to Unity, I have a question regarding shooting bullets in the direction that the sprite is moving. I have looked at the following 2 tutorials:

https://www.youtube.com/watch?v=8uD2ATIot0A

https://www.youtube.com/watch?v=1mw1ufZq1N4&t=111s

while both come close to a solution, I can't seem to figure out the best way to achieve the desired effect. Both of the tutorial above use either a cursor to aim and shoot or directional keys to rotate the sprite.

I need the sprite to be able to move in 8 directions with animations using the arrows keys and using the Fire1 button to shoot a bullet. I do not know how to get the bullet to travel in the direction the sprite is moving at the time it is fired.

Any help or directions is much appreciated.

Thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Magso · Nov 23, 2019 at 01:53 AM

It depends on how you are moving the bullets. Using Translate() or AddForce(transform.up) the bullet can be instantiated to the same rotation as the player. Using AddForce(Vector2) the bullet needs the global values depending on which way the player is facing. e.g.

 if(player.GetComponent<SpriteRenderer>().sprite.texture == facingRightTexture)
     {
     bullet.GetComponent<bulletScript>().addForceVector2Variable = Vector2.right * speed;
     }

and on the bullet

 GetComponent<Rigidbody2D>().AddForce(addForceVector2Variable, ForceMode2D.Impulse);

Comment
Add comment · Show 2 · 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 vbjack72 · Nov 23, 2019 at 03:27 PM 0
Share

@$$anonymous$$agso Thank you for replying so quick.

Here are the functions I am using to move the character

 void ProcessInputs()
     {
         movementDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
         movementSpeed = $$anonymous$$athf.Clamp(movementDirection.magnitude, 0.0f, 1.0f);
         movementDirection.Normalize();
     }
 
     void $$anonymous$$ove()
     {
         player.velocity = movementDirection * movementSpeed * speed;
     }


I am using animations to give the appearance of the character changing direction. The problem is I need the bullet to travel in the direction the character is facing. Like I mentioned above the tutorials are using rotation or a pointer to make the projectile travel in the direction of the rotated GameObject (sprite). I was thinking of using a child Object and rotating that in the direction of the inputs/character but I can't seem to get that working either.


Here is the script that I used from one of the tutorials above.

     public float bulletSpeed = 8.0f;
 
     // Update is called once per frame
     void Update()
     {
         Vector3 pos = transform.position;
 
         Vector3 velocity = new Vector3(0, bulletSpeed * Time.deltaTime, 0);
 
         pos += transform.rotation * velocity;
 
         transform.position = pos;
 
     }


Any help is appreciated.

avatar image Magso vbjack72 · Nov 24, 2019 at 01:07 AM 0
Share

You need a way to check the direction the player is facing, in this case probably the sprite's texture. You can change Vector3 velocity = new Vector3(0, bulletSpeed * Time.deltaTime, 0) to AddForce(transform.up, Force$$anonymous$$ode2D.Impulse) and set the rotation to 0, 45, 90, etc by checking the sprite texture. Here's another e.g. I probably should've put in my original answer.

 void FixedUpdate()
     {
     GetComponent<Rigidbody2D>().AddForce(transform.up, Force$$anonymous$$ode2D.Impulse);
     if(player.GetComponent<SpriteRenderer>().sprite.texture == facingRightTexture)
         {
         transform.eulerAngles.z = 90;
         }
     }

avatar image
0

Answer by vbjack72 · Nov 25, 2019 at 06:36 PM

Hello @Magso

Based on your feedback, I used the animator to get the direction of the character. using that direction I was able to apply the correct rotation and offset to the bullet object to get the desired effect. Below is the code that I am using to do this.

         private void Update()
             {
                 cooldownTimer -= Time.deltaTime;
         
                 if (Input.GetButton("Fire1") && cooldownTimer <= 0)
                 {
                     // Debug.Log("Pew!");
                     // Shoot the gun
                     cooldownTimer = fireDelay;
                     int shotRotDeg = 180;
         
                     int aniHor = (int)Math.Round(animator.GetFloat("Horizontal"));
                     int aniVer = (int)Math.Round(animator.GetFloat("Vertical"));
                     float offsetX = -0.14f;
                     float offsetY = -0.4f;
         
                     // Down        0, -1
                     if (aniHor == 0 && aniVer == -1)
                     {
                         shotRotDeg = 180;
                         offsetX = -0.14f;
                         offsetY = -0.4f;
                     }
                     // DownRight    1, -1
                     else if (aniHor == 1 && aniVer == -1){
                         shotRotDeg = 225;
                         offsetX = 0.55f;
                         offsetY = -0.1f;
                     }
                     // Right        1, 0
                     else if (aniHor == 1 && aniVer == 0)
                     {
                         shotRotDeg = 270;
                         offsetX = 0.55f;
                         offsetY = 0.12f;
                     }
                     // UpRight        1, 1
                     else if (aniHor == 1 && aniVer == 1)
                     {
                         shotRotDeg = 315;
                         offsetX = 0.4f;
                         offsetY = 0.5f;
                     }
                     // Up        0, 1
                     else if (aniHor == 0 && aniVer == 1)
                     {
                         shotRotDeg = 0;
                         offsetX = 0.25f;
                         offsetY = 0.5f;
                     }
                     // UpLeft        -1, 1
                     else if (aniHor == -1 && aniVer == 1)
                     {
                         shotRotDeg = 45;
                         offsetX = -0.4f;
                         offsetY = 0.5f;
                     }
                     // Left        -1, 0
                     else if (aniHor == -1 && aniVer == 0)
                     {
                         shotRotDeg = 90;
                         offsetX = -0.55f;
                         offsetY = 0.12f;
                     }
                     // DownLeft    -1, -1
                     else if(aniHor == -1 && aniVer == -1)
                     {
                         shotRotDeg = 135;
                         offsetX = -0.55f;
                         offsetY = -0.1f;
                     }
         
                     // Vector2 shootingDirection = gun.transform.localPosition;
                     Vector3 offset = new Vector3(offsetX, offsetY, 0);
                     // shootingDirection.Normalize();
         
                     shootingDirectionX = animator.GetFloat("Horizontal"); //shootingDirection.x;
                     shootingDirectionY = animator.GetFloat("Vertical"); // shootingDirection.y;
         
                     GameObject bullet = Instantiate(bulletPrefab, player.transform.position + offset, Quaternion.identity);
                     bullet.transform.Rotate(0, 0, shotRotDeg); // Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
         
                 }



I am not sure if this is the best way to go about this, but for now it is working and I can start looking in to other parts of the game. Thank you so much for your help and guidance, it really helped me out.

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

238 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 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

Having Issues Rotating 2d Sprites to face another 2d Object 3 Answers

Stop player rotation when there is no movement 1 Answer

Joystick 2D Sprite Rotation and Movement 0 Answers

How can i edit this code to rotate my player to fixed angles when a key (or a combo of keys) is pressed? 1 Answer

Convert player input to angle/rotation? 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