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 EvanCheddar · Apr 19, 2018 at 06:26 PM · instantiate2d gameshootingprojectilediagonal

How to make projectiles shoot diagonally?

Hello all, Im trying to make a 2D "spread shot" where 3 projectiles are instantiated in 3 different directions. One straight forward and 2 diagonals up and down. I feel like there is a simple way to do this that i'm not figuring out.

Here's my code:

 public GameObject Bulletprefab;
 
 void Update ()
     {
         if (Input.GetKeyDown (KeyCode.RightShift)) 
         {
             Shoot ();
         }
     }
     public void Shoot()
     {
         GameObject Bullet = Instantiate (Bulletprefab, transform.position, transform.rotation);
     }
 
 }
 
 
 
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
3
Best Answer

Answer by Kciwsolb · Apr 19, 2018 at 06:51 PM

There are lots of ways you could do this. An easy visual way is to create a reference to 3 public Transforms in your code.

 public Transform shootDirection1, shootDirection2, shootDirection3;

Then create 3 empty GameObjects in your scene (probably as children of your gun). Line these up so they point in the directions that you want your projectiles to shoot out (one with the z axis pointing forward, one angled up, one angled down). Then drag a reference of the Transform of these GameObjects to the public Transforms on your script. Then in your script make 3 different Instantiate calls for each of your shooting directions:

 Instantiate (Bulletprefab, shootDirection1.position, shootDirection1.rotation); //Do this for each shootDirection

And there you go! You should be shooting in 3 different directions! :)

Comment
Add comment · Show 3 · 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 EvanCheddar · Apr 19, 2018 at 07:26 PM 1
Share

The projectile doesn't travel in the diagonal direction, it just gets instantiated rotated on an angle. I want it to fire and move on a diagonal path

avatar image EvanCheddar · Apr 19, 2018 at 09:30 PM 1
Share

I used your idea to get the rotation, then I used a vector 3 for the speed and combined X and Y speeds to get the diagonal.

avatar image Kciwsolb · Apr 19, 2018 at 11:28 PM 0
Share

$$anonymous$$y only goal was to get it instantiated and rotated properly. I assumed you had a script for your projectile that was moving it along its local forward (z) axis. If you don't have a separate script for that, here is a line of code you could add that would work (it assumes your projectile prefab has a Rigidbody):

 /*Do the below for each shootDirection*/
 Rigidbody projectile1Rb = Instantiate (Bulletprefab, shootDirection1.position, shootDirection1.rotation).GetComponent();    
 projectile1Rb.AddRelativeForce(new Vector3(0.0f, 0.0f, projectileSpeed), Force$$anonymous$$ode.VelocityChange);

Okay so, I think learning is important, so here's what this does. First we create a Rigidbody variable, and then we store the reference to the Rigidbody returned by the GetComponent we call on the GameObject returned by Instantiate(). We then call AddRelativeForce on that Rigidbody to get our projectile moving. We use the relative version on AddForce so that it moves according to the projectile's local rotation. We use Force$$anonymous$$ode.VelocityChange to instantly change its speed as apposed to slowly accelerating it. If these have helped you, please accept the reply as the answer. Good luck! :)

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

108 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

Related Questions

Slow automatic shooting script? 2 Answers

How to shoot a bullet and animate at the same time??? Its a 2D shooter c# 3 Answers

How do I stop a 2D projectile when it reaches a point 3 Answers

How do I make a 2d bullet object move towards the players original position when it is spawned? 2 Answers

Shooting a bullet object toward the mouse position in a 2D. 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