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
1
Question by leonalchemist · Sep 28, 2011 at 08:06 PM · instantiateanglebulletmouseposition

Shoot at an angle 2D

i have a 2D game, using the x,y axes to move around the screen and a player that shoots bullets torwards the screen.

i got different scripts for the missile script, either addForce to the bullet or make the position of the bullet go towards the mouse

Now how to i make it so i shoot at an angle like instead of shooting one bullet directly towards the mouse when i level up i shoot instantiate 2 bullets at a fixed angle away from the mouse position? thx u

 //Calcualtes where the mouse is on the scren
 var mousePos : Vector3 = Input.mousePosition;
 mousePos.z =- (transform.position.z - Camera.mainCamera.transform.position.z - 14);
 var worldPos : Vector3 = Camera.mainCamera.ScreenToWorldPoint(mousePos);
 var targetDelta = (worldPos - transform.position);
 
 //Shoot torwards mouse
 var Missile : Rigidbody = Instantiate(MissilePrefab, transform.position, Quaternion.identity);
 var targetDelta = (worldPos - transform.position);
 var launchVelocity = targetDelta.normalized * MissileSpeed;
 Missile.velocity = launchVelocity;


OR this is another way of shooting towards the mouse if u prefer that way

 var Missile : Rigidbody = Instantiate(MissilePrefab, transform.position, transform.rotation);
 Missile.rigidbody.AddForce(targetDelta * 300);
 

EDIT

This is the new code which DOES shoot at an angle but the closer the mouse pointer the slower the bullet speed, i think i had this problem b4 but didnt notice it somehow O_x i get its because the mouse position changes and im multiplying it to the distance of the mouse position, just dunno how to fix that.

 var eulerAngles : Vector3;

in UPDATE Function()

 //Calcualtes where the mouse is on the screen
 var mousePos : Vector3 = Input.mousePosition;
 mousePos.z =- (transform.position.z - Camera.mainCamera.transform.position.z - 14);
 var worldPos : Vector3 = Camera.mainCamera.ScreenToWorldPoint(mousePos);

EDIT again(my bad i put worldPos instead of targetDelta here which was wrong) EDIT yet AGAIN :) :) :) :) cause I FIXED IT!! woot! used targetDelta.normalized and it works, which is surprising since im dont rly know wat it does and it was my 1st guess which i didnt even know where i should have started xDDDD

 var force : Vector3 = (targetDelta.normalized * 800);

 //Instantiate one bullet 20 degrees right of the mouse
 eulerAngles.z = -20;
 var newForce : Vector3 = Quaternion.Euler(eulerAngles) * force;
 var Missile : Rigidbody = Instantiate(MissilePrefab, transform.position, transform.rotation);
 Missile.rigidbody.AddForce(newForce);

 //Instantiate one bullet 20 degrees left of the mouse
 eulerAngles.z = 20;
 newForce = Quaternion.Euler(eulerAngles) * force;
 Missile = Instantiate(MissilePrefab, transform.position, transform.rotation);
 Missile.rigidbody.AddForce(newForce);
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
4
Best Answer

Answer by syclamoth · Sep 28, 2011 at 11:50 PM

The easiest way would be to put an offset in your AddForce script! There are a few ways of doing this, but the one I like involves taking the rotation from another transform in the scene-

// this should be set in the inspector. public Transform bulletAngleOffset;

Vector3 force = whatever you set it to;

// This rotates the force by the angle offset object's local rotation. Vector3 newForce = bulletAngleOffset.localRotation * force;

bullet.rigidbody.AddForce(newForce);

EDIT:

Alternatively,

// At the top public Vector3 eulerAngles;

// in your shooty script Vector3 force = whatever; Vector3 newForce = Quaternion.Euler(eulerAngles) * force;

// now use newForce to shoot things however you would have used force

Alternatively alternatively,

// at the top var eulerAngles : Vector3;

// in your shooty function var force : Vector3 = whatev; var newVorce : Vector3 = Quaternion.Euler(eulerAngles) * force;

// and so on.

Then you don't have to worry about putting in a new transform, but you have to know exactly what angle you want it to shoot at.

Comment
Add comment · Show 13 · 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 leonalchemist · Sep 29, 2011 at 12:45 AM 0
Share

umm appreciate the help but dont understand it fully, just to make things clearer is this like a grenade effect? and not sure wat kind of force i should apply either. but let me make things much simpler for the both of us and edit my question with code, and also if its ok javascript would be better, the code above isnt a problem just for the future.

avatar image syclamoth · Sep 29, 2011 at 01:32 AM 0
Share

You said you were using Addforce! Either way, the code will work for transfor$$anonymous$$g vectors however you choose to use the resultant value.

avatar image leonalchemist · Sep 29, 2011 at 01:37 PM 0
Share

i am using either AddForce or that other script, whichever makes my live easier i'll use rly. but any help using my script above, and cant rly tell wat to set the bulletAngleOffset to either, im a bit lost :/

avatar image syclamoth · Sep 29, 2011 at 01:39 PM 0
Share

you set bulletAngleOffset in the editor, by dragging a transform onto the little box! Then, to change your angle, rotate that transform until the angle is correct. You can use this with things like Transform.LookAt on your secondary transforms, and make secondary auto-turrets! I know that's not what you wanted here, I'm just suggesting other things you could do with this code.

avatar image leonalchemist · Sep 29, 2011 at 01:51 PM 0
Share

wat im saying was where should that bulletAngleOffset be set to, the player's position? the mouse position? and doesnt seem like it would work in my case cause watever the rotation its gonna aim towards the mouse.

and i already have auto turrets in another game :P

Show more comments
avatar image
0

Answer by leonalchemist · Sep 29, 2011 at 11:51 PM

Anyway im posting the answer here for anyone thats interested, thx to syclamoth for the help and so far this script seems to work perfectly fine, now i can sleep soundly tonight knowing i managed to fix it :P

 //Calcualtes where the mouse is on the screen
 var mousePos : Vector3 = Input.mousePosition;
 mousePos.z =- (transform.position.z - Camera.mainCamera.transform.position.z - 14);
 var worldPos : Vector3 = Camera.mainCamera.ScreenToWorldPoint(mousePos);

 var force : Vector3 = (targetDelta.normalized * 800);

 //Instantiate one bullet 20 degrees right of the mouse
 eulerAngles.z = -20;
 var newForce : Vector3 = Quaternion.Euler(eulerAngles) * force;
 var Missile : Rigidbody = Instantiate(MissilePrefab, transform.position, transform.rotation);
 Missile.rigidbody.AddForce(newForce);

 //Instantiate one bullet 20 degrees left of the mouse
 eulerAngles.z = 20;
 newForce = Quaternion.Euler(eulerAngles) * force;
 Missile = Instantiate(MissilePrefab, transform.position, transform.rotation);
 Missile.rigidbody.AddForce(newForce);
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Randomly instantiate bullets within a certain angle based on look direction 1 Answer

Instantiate Angle from a Child Object 1 Answer

Instantiating a pattern in front of the player? 1 Answer

Is Instantiating bullets/many objects always bad for performance? 1 Answer

Gun Projectile Shooting In Wrong Direction (Javascript) 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