- Home /
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
Your answer
Follow this Question
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