Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 master2080 · Oct 11, 2014 at 10:20 PM · rigidbody2dvelocityangle

Figure out velocity under an angle

Hello,

I am trying to achieve a shot being fired from a player under an angle. The angle is determined by the mouse(if the mouse is in front and above the player then he will shoot at approximately 45 degrees) I have figured out how to calculate the exact angle of the shot, but what I can't figure out is how I can make that shot move towards where the mouse is located during firing. This is all in 2d using Rigidbody2D. Can someone help me?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LSPressWorks · Oct 11, 2014 at 10:51 PM

There are several ways to go about this. One is to just have the projectle look at the target coords. Then push it forward. I prefer this method as unity is essentially already doing this stuff.This is 3d though. In 2d, it's a little different then this code. But the underlying concept is the same.

 object.transform.lookat(target);
 object.rigidbody2d.addforce(transform.forward * speed * modifier);


Since you're 2d, this already answered question explains the details of how that works...

http://answers.unity3d.com/questions/603413/how-to-add-forward-velocity-to-a-rigidbody2d.html

The other way is fetching world to screen co-ordinates with something like a raycast, and then using Pythagorean theorem to fetch the x,y of the projectile. The good thing about being in 2ds is I don't know if you even have to use a ray or translation.

This is from a java site, I used it in one of java platform games. I did that instead of using the trig angles, as it wound up being a little cheaper. NitPicking cheap in unity is pointless though, so use whatever works easiest for you.

Both examples cover the underlying constructs, as well as providing easily portable code for the matter at hand.

http://stackoverflow.com/questions/6208833/java-game-development-look-and-shoot-at-mouse-coordinates

http://stackoverflow.com/questions/16152710/making-image-travel-towards-mouse-cursor

this is specifically the part you're wanting...This is platform java though, but as I said, like a four minute port...(in fact I just did it for ya...might not be 100%). Should get you started. The pythag, is the a^2 + b^2 = c^2. It's from that, or the atan, or the vector3.forward that you can get the bullet moving.

 var  deltaX :double = player.getX() - point.getX();
 var  deltaY : double = player.getY() - point.getY();
 var magnitude : double = Mathf.sqrt(deltaX*deltaX + deltaY*deltaY);
 
 // It's possible for magnitude to be zero (cursor is over the person)
 // Decide what you want the default direction to be to handle that case
 var xVelocity :double = 1;
 var yVelocity :double = 0;
 if (magnitude != 0) 
    {
        xVelocity = deltaX / magnitude;
        yVelocity = deltaY / magnitude;
    }
 
 //With the velocities computed when you first create the //bullet, at each tick you just need to use
  xPosition += xVelocity and yPosition += yVelocity.


Anyways, I hope this is what you were asking for. Good luck, and remember, this stuff takes time to learn, and unity has it's own curve...

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

2 People are following this question.

avatar image avatar image

Related Questions

,Rotate a gameobject around another while being attracted by its gravity 1 Answer

Modifying a kinematic rigidbody2D.velocity makes player to go through the wall 0 Answers

Weird Rigidbody2D.velocity.x problem, float out of control 0 Answers

Rigidbody 2D has velocity but isn't moving after Unity 5 upgrade 2 Answers

NullReferenceException: UnityEngine.Rigidbody2D.get_velocity() 0 Answers


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