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 DrakesGames · Jul 24, 2020 at 11:07 AM · movementrigidbody2dphysics2drigidbody.addforcetransform.translate

Move an object via Rigidbody2D Physics and Transform at same time

,Apologies in advance if this is dumb, been scratching my head all day on this one and my guess is I have probably pigeon holed myself in how I am thinking about it / need help.

I am building a 2d endless runner type game. The way the game is structured, I have the player character on the left side of the screen. The player's X position in world space dosen't move, rather the player stays still and their "movement speed" is just a value that drives how fast the background scrolls etc...

Up until now, the way that I have handled objects' (enemies, obstacles, etc...) movement relative to the player is by adding a component to them that gets a reference to the player's speed and translates the object based on that. So far, it has been simple, clean, working well.

The problem is in how this overlaps with the enemy movement. The enemies are moved via dynamic rigidbodies (Enemy AI controllers call a mover which applies force). At first glance, these two different movement methods appear to work fine together gameplay-wise. The enemy moves according to their AI using the Rigidbody, all the while being translated towards / away from the player based on the player's speed (as if the player were walking towards / away from the enemy).

My understanding is that this is bad practice. Specifically, the docs / other forum posts describe that moving an object simultaneously via transform and rigidbody isn't advisable which makes sense to me (translating a transform is directly manipulating the game object's position whereas rigidbody.addforce utilizes the physics engine etc... etc...). I profiled my project and I can definitly see that there is some CPU drain on the Physics step correction as the transform and RB become out of whack.

My questions for you all:
1) What is a better way to accomplish this type of movement (enemy's desired movement based on AI + movement relative to the player's "speed relative to world") using the physics system only (get rid of transform.translate script)? I feel like I am missing something obvious here. 2) Is it bad if I just leave things the way they are? Assuming I am happy with the gameplay, what are downsides I might be missing? Is the performance going to blow up into something really noticeable?

Stuff I have tried so far wrt (1): - Rigidbody.MovePosition to replace transform.translate: I dont think this is right either given I am using dynamic rigidbodies - Rigidbody.AddForce to replace transform.translate: This wasn't working well / I can't get my dumb brain around how this should work. If I just add a force that is proportional to the player speed value each FixedUpdate call, I wont be able to stop adding the force when I have reached the desired velocity because the velocity of the enemy is also being driven by the enemy's AI. For example: If the enemy's controller is telling it to move right by some magnitude M every frame, but the player's "speed" would mean that the enemy should net be moving left, how would I implement that properly? Track the amount of velocity applied to the object by each type of force?

Anyways, any help would be greatly appreciated here. I need to sleep on this.

Unity Version: 2019.4.3f1 Target Platform: Android

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

0 Replies

· Add your reply
  • Sort: 

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

201 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 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

About Rigidbody2D.AddForce's duration... 2 Answers

Stop movement of rigid bodies 2D after collision 1 Answer

Rigidbody2D doesn't move 0 Answers

Child GameObject can't use MovePosition on parent's Rigidbody2D? 3 Answers

Weird behaviour after Collision? 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