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 chrisantemo · Jan 06, 2020 at 03:03 PM · rotationshooting

Shoot in the direction of an object 2d

I am trying to make a turret that shoots at a specified target. However, all things I have tried have yielded strange results. I first tried from instantiating the bullet from the turrets rotation, which follows the player. This caused the bullet to shoot in a direction that only lined up with the player when above if using transform.up and right with transform.right. When following tutorials online I got even stranger results, with the bullet going in figures of eight. After trying to tweak the scripts, I got it to shoot in the same direction every time. I also tried to change the rotation from both the bullet and shooter side, but all my solutions have failed. I am sorry if this is such a simple problem, but I have found no answer.

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
1
Best Answer

Answer by unity_ek98vnTRplGj8Q · Jan 06, 2020 at 03:42 PM

This is generally the approach I would take, although I'm sure there are ways you can do it in less lines of code. This assumes your bullet points to the right by default, but if it doesn't you can tweak the code or the bullet prefab to fix this.

 void Shoot(){
     //Get direction vector pointing at target
     Vector2 directionToTarget = target.transform.position - transform.position;
 
     //This assumes that your bullet sprite points to the right
     //Get the angle above the horizontal where the target is
     float angle = Vector3.Angle(Vector3.right, directionToTarget);
     //This will always be positive, so lets flip the sign if it should be negative
     if(target.transform.position.y < transform.position.y) angle *= -1;
 
     //Create a rotation that will point towards the target
     Quaternion bulletRotation = Quaternion.AngleAxis(angle, Vector3.forward);
     //Spawn bullet
     Instantiate(bulletPrefab, transform.position, bulletRotation);
 }
Comment
Add comment · Show 4 · 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 chrisantemo · Jan 06, 2020 at 04:09 PM 0
Share

Unfortunately, this has not worked. I remain with one of my old problems, even if i remove all other unimportant parts of both the bullet and shooter. The bullets will shoot down if I am above or below it, and up if I am to the left or right. They will also shoot left or right depending on the diagonal. I have experienced this problem many times before, is there something I am doing wrong?

avatar image unity_ek98vnTRplGj8Q chrisantemo · Jan 06, 2020 at 05:06 PM 0
Share

What is your code to actually move the bullet?

avatar image chrisantemo unity_ek98vnTRplGj8Q · Jan 06, 2020 at 05:14 PM 0
Share
 public class bullet : $$anonymous$$onoBehaviour
 {
     public float speed;
 
     void Update()
     {
 
         transform.Translate(transform.up * speed * Time.deltaTime);
 
 
     }
 }
avatar image unity_ek98vnTRplGj8Q chrisantemo · Jan 06, 2020 at 05:39 PM 0
Share

Ahhhh took me a while to figure out the issue. transform.Translate() can take a "Space" parameter, which if not specified defaults to local space ins$$anonymous$$d of world space, which is what we want.


Try this ins$$anonymous$$d: NOTE - In my above code I assumed the default direction for the bullet was to the right, so that's what I have here. If the default direction is actually up, like your code seems to indicate, we may have to change a couple more things

 public class bullet : $$anonymous$$onoBehaviour
 {
     public float speed = 2;
 
     void Update()
     {
         transform.Translate(transform.right * speed * Time.deltaTime, Space.World);
     }
 }



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

154 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

Related Questions

bullet shot from Y axis while sprite 2D rotates on z axis 1 Answer

Shooting with variable vector3 0 Answers

Shooting mechanics in 2.5D game 0 Answers

Player jitters when moving. 1 Answer

Gun not firing in certain direction 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