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 neigaard · Jul 11, 2014 at 10:36 AM · rigidbody2dphysics2d

Adding force to RigidBody2D and at the same time make sure velocity is always in the direction of transform.up

Hi,

I have a 2D spaceship that fly around and it occasionally encounters some asteroids (or other ships) and the way I have implemented this is by converting user input to a Vector2D like this:

 Vector2 movement = new Vector2(0, currentPlayerSpeed);

The "currentPlayerSpeed" is calculated by the ship max speed and added in a acceleration over time, and I then add that Vector2D to the ships transform in the Update method like this (the ship always fly "up" relative to its own rotation):

 transform.Translate(movement);

I rotate the ship based on input (and added in some max rotation force) and the ship always fly in the direction of "up" with the current velocity, and this means that it does not fly even the the least sideways never ever. This is the way I want it to work, but... I would also like the ship to bounce of any asteroids (or other ships) it hits and I would like to use the build in physics for this.

I have converted my Update to FixedUpdate and I now do this to move the ship instead:

 rigidbody2D.AddForce (transform.up * currentPlayerSpeed);
 if(rigidbody2D.velocity.magnitude > maxShipSpeed)
 {
     rigidbody2D.velocity = rigidbody2D.velocity.normalized * maxShipSpeed;
 }

So with this I get the bounce off effect I want, but obviously the ship now fly sideways most of the time as the force is not directly translated to the ships velocity and manipulating the rigidbody2D.velocity directly gives me strange results so I have no idea how to do this. I have tried with the following code:

 float magnitude = rigidbody2D.velocity.magnitude;
 rigidbody2D.velocity = transform.up * magnitude;

and this makes the ship fly as I want it to, but obviously it also cansels out the collision forces so I am back to square zero. Any help/ideas are welcome as I am quite stuck with this.

Thank you Søren

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 KMKxJOEY1 · Jul 11, 2014 at 01:33 PM

The way that I would go about doing this would be to stick with constraining the user input movement to the Y axis (like you did first) and then have event based physics on collision. E.G.

 void OnCollisionEnter(Collision col)
 {
     //calculate the rigidbody.AddForce here based on the data recieved from col
     //this way the Physics is still applied, but the USER input is still only the Y axis
 }
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

22 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

Related Questions

Simplest way to deaccelerate a Rigidbody2D object 1 Answer

My player is unexplainably destroyed when collides with a collider 0 Answers

2D Physics Problem? 1 Answer

Player Knockback Without Physics 0 Answers

Jittery physics with rigidboyd2d, how to solve? 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