- Home /
Calling Rigidbody.MovePosition, no movement at all
Okay, so, this is weird. I added a line of code to my project last night that will move an object about one unit to the right every frame with Rigidbody.MovePosition(), and everything worked fine and dandy. Saved, quit, log back in the next day, and it's not doing anything, even though the line of code is apparently being called.
Using Transform.translate works, but I desire the collision detection that comes with doing things with a Rigidbody. Manually assigning the transform's position every frame works, and allows for semi-functional collision detection, but I don't want to use that, because I desire smooth and accurate collision detection for this game.
I don't see why this would work one night and the next, it doesn't. I've checked the GameObject; the rigidbody is still attached to it, and it is not Kinematic, nor is it using gravity. Any help would be greatly appreciated.
Update:
To put this into context, this game is top-down and two-dimensional. Here is my movement code:
this.rigidbody.MovePosition(new Vector3(this.rigidbody.position.x + this.currentSpeedX,this.rigidbody.position.y + this.currentSpeedY,this.rigidbody.position.z));
(currentSpeedX and currentSpeedY are set to 1.0f if the necessary keys are pressed and 0f otherwise; this is being called every frame)
This does not work. However,this works perfectly:
this.transform.position = new Vector3(this.transform.position.x + this.currentSpeedX,this.transform.position.y + this.currentSpeedY,this.transform.position.z);
Here is the relevant information from my player GameObject:
A code piece and a picture of your prefab would be helpful.
I have updated my original post with relevant information.
Perhaps freezing z is the issue? In unity, y is up and the x-z plane is the horizontal plane.
Unfreezing Z didn't do anything. Everything is angled and positioned with X being left and right and Y being up and down, so I didn't think that would work.
Collider stuck in floor?
Using GetComponent appropriately to capture the actual Rigidbody in question?
Answer by mrEagle · Dec 07, 2017 at 03:18 PM
Being late to the party:
I forgot to turn off some constraints on my rigidbody. As they are folded by default in the inspector (and you may have folded your rigidbody component altogether), you may want to double check these options :)
My experience is that Rigidbody.movePosition is agnostic to where it is called, the effect is the same for Update and FixedUpdate. I guess the morale of the answers so far, is that if your object does not move, the physic engine is probably preventing it for good reasons.
Answer by codecranker · May 11, 2013 at 12:14 AM
Are you calling MovePosition in Update or FixedUpdate?
The doc says it should be in the FixedUpdate. Thats the update for physics based movements. http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.MovePosition.html
Also, can you try checking the 'Is Kinematic' on your RigidBody component to see if that makes any difference?
Answer by Rainbirth · Jul 20, 2016 at 02:27 PM
I had the same problem to solve it make sure that it isn't a child of an object that is moving its position in a Fixed update.
I had this situation that was blocking movement
Parent -> moving its transform with one script in its fixed update. ----> rigidbody child using the moveposition in its fixed update <--- this doesn't work.
To fix it just use Update in the parent to move its position.
I hope this helps.
Answer by IgorAherne · Mar 08, 2019 at 01:57 PM
Also check that rigidbody's "freeze position" is not ticked (in its Inspector window).