Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Pseu · Jul 06, 2015 at 06:13 PM · 2drotationaddforceangleaddforceatposition

AddForce based on Angle/Rotation (2D)

I have a small game (2D) where the player chooses an angle and power to try and 'jump' a block in front of them.

My problem is that I have been experimenting with various types of AddForce and inputting different vectors to achieve different results. All do function, but none of them function as planned.

My 'FinalAngle' is achieved by this:

 finalAngle = playerCharacter.transform.localEulerAngles;

Which outputs something in the range of (0,0,0) and (0,0,60) depending on where the player stopped the character rotation and my power is an int between 1 and 100.

Almost all attempts lead to either the cube flying massively off screen, or barely moving.

I'd prefer if someone could just guide me through how I can apply these two variables together to get a nice arcing force. Here is the closest I have gotten on my own:

 playerBody.AddForceAtPosition(new Vector2 (finalAngle.z, finalAngle.z), new Vector2 (playerCharacter.transform.position.x, playerCharacter.transform.position.y), ForceMode2D.Impulse);

However it doesn't take into account the power, flies the cube off screen and never arcs back down. But at least it seems to get the right angle! xD

Any help very much appreciated, especially if you can point me in the direction of how I can 'predict' (Preferably through some sort of GUI element) how an AddForce will work.

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 Pseu · Jul 07, 2015 at 01:12 AM

For those interested I actually managed to create what I needed by changing the velocity of the object directly instead of using AddForce and then fiddling with values a little. The end code ended up looking like this:

 void JumpFunction(){
     if (!jumpActive){
         Rigidbody2D playerBody = playerCharacter.GetComponent<Rigidbody2D>();
         playerBody.isKinematic = false;
         playerBody.angularVelocity = power*10;
         playerBody.velocity = new Vector2 (finalAngle.z*powerAfterConvert, finalAngle.z*powerAfterConvert);
         jumpActive = true;
     }
     if (jumpActive){
         Rigidbody2D playerBody = playerCharacter.GetComponent<Rigidbody2D>();
         if (playerBody.velocity.x < 0.1){
             if (playerBody.velocity.y < 0.1){
                 currentState = GameStates.RESET;
             }
         }
     }
 }

Power is now a float and after the player gets a power (Only int 0-100 possible), I then divide it by 100 so it doesn't fly off the screen.

It isn't a fantastic solution and needs tweaking, but it is certainly a LOT better than where I was it.

Comment
Add comment · Show 1 · 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 RoCkHeArTeD · Oct 05, 2015 at 03:28 PM 0
Share

I tried your solution, the object wouldn't move at all :/ Care to help me please?

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

Angle to Rotation 2 Answers

How can I limit the rotational speed of a gameObject tracking my mouse? 1 Answer

Add Force to the right of the rigidbody, not right of the screen 1 Answer

Rotating 2D sprite to match surface. 0 Answers

2D : Rotation of Object With x-y Axis 2 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