- Home /
Rigidbody.AddForceAtPosition not working how I expect... :(
I'm trying to use Rigidbody.AddForceAtPosition, based on 3 positions on a ship. Applying the position's .forward and position to apply the force. The main one in the back/center works perfect, the ship flies forward. The left and right cause it to ship to spin as if tethered to something about 10 meters behind it. Based on how I have this lined up, I'm not sure why it behaves this way. Below is the code, transform orientation and video to how they move from tapping forward, and then turning.
What I'm hoping for, is that when I put force on the left side, the left side will push forward, turning the ship to the right. Not spinning as if tethered on a may pole.
Video: https://www.youtube.com/watch?v=i3SrMiq-yN8
I would guess that you need to take a look at your model/ship setup in the scene. It looks as if the rigid body is calculating the centre of mass behind the ship. You could set the centre of mass for the rb in the Start method and work around it.
Also, you should really apply physics force in FixedUpdate. Detect input and calc a force in Update but apply it in FixedUpdate.
Thank you, I will try to test this today
From your video, it is hard to say when you press right or left. Looks like you active left thrust first, then right ? Also, the camera is moving too much to get a good understanding of the movement. It would be more helpful to have an editor view with a fixed camera and visible inspector to read the transform.
Btw, I support @Warmed$$anonymous$$ints, this is certainly a center of mass issue. Do you have any extra colliders attached to your gameobject (including children) ?
I checked the center of mass on the Rigidbody's info, and its centered to the ship. I paused at a couple points to make sure the world center matched up. But I agree it does seem to be here. Next I'll check sub objects for weight differences but I would have assumed that any colliders/rigid bodies would have been tallied in the parent rigidbody.
Answer by DanVioletSagmiller · Sep 26, 2019 at 03:37 PM
Facepalm (sort of) but something to watch out for. @Mouton said it was hard to tell with the camera movement. So in a test this morning, I turned off Cinemachine, and just used a static camera. Worked Perfectly.
The problem was cinemachine trying to track and follow the object over a sky box background. Cinemachine was doing things like look ahead and other tactics to follow and work with the ship. I strongly recommend NOT testing any physics things with no real BG and a highly dynamic camera controller. It was actually spinning in place in the video, but look ahead was trying to move with it truly screw up its appearance.
If I had tried this over a 3D background, like terrain, it would have stood out that the camera was moving its position as well.
I had a feeling about this... :)
Always try to reproduce your issue in the simplest environment to prevent noise.
Glad you found the problem, happy coding !
Btw, HOW do you manage to insert empty lines in your posts ??
heh heh... I'm not sure if I can post the text to show it, so here is an image link. (html Line breaks, two of them) link text
Your answer
Follow this Question
Related Questions
Have player fall down with platform? 0 Answers
Smooth dashing issues 0 Answers
Freeze Position + collision? 0 Answers
Make a object move forwards 1 Answer