Transform Translate does not detect collision

I have game which character bounces from the platforms. The movement happens with finger and i am using Vector3.Lerp to move the character. Sometimes if the movement so fast character goes inside of platform. I know i need to use physics to avoid this but i cannot use Rigidbody2D.MovePosition because character has gravity. I tried to use rb.velocity but I couldn’t set with finger movement. Is there anything you can suggest?

With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

I cannot .MovePosition() because of the gravity. I am using gravity on my character so if i use .MovePosition() that time gravity is not working.

If your finger says “go right” and gravity says “go down,” what exactly do you expect to happen?

I want it to go towards the lower right diagonal. When the character collide with the platform its automatically jumping so player cannot change this. The player can only change the X axis.

Is using Rigidbody.position instead of transform.position better for detecting physics?

Sorry to say but your conclusions are just plain wrong here. You say you cannot use this (which is just asking to move physically to a position) but you think you can use Transform to just move to a specific position and not pass through the space.

The question itself is invalid. Neither is “better” nor more appropriate than the other.
A Rigidbody2D moves with its velocity. Velocity is integrated to update its position then the Rigidbody2D position is written to the Transform and renderers use this to render so you then see it.

As a dev, you should manipulate the velocity, preferably indirectly via adding forces or very carefully directly by modifying the velocity but when doing so directly, you need to understand what you might be writing over. Write 0 to the Y component of it, that will mean any velocity applied via the default gravity is gone.

If you set the Transform or Rigidbody2D position, you are instantly teleporting to that position (think Star Trek). You didn’t move in the space between where you were to where you want to be and you likely are teleporting into the ground (ouch!) or even skip completely over a collider.

If the above is still confusing then you absolutely do need to find some tutorials on how to use 2D physics, specifically a Rigidbody2D, to cause movement.

I did my movement with velocity, thanks for your answer. But still I want to ask one question. Is there any way to use .MovePosition() with gravity?

For example, gameobject is moving only X axis with .MovePosition() and this gameobject has gravity, so when I try to move this object on X axis, at the same time can gravity apply?

When I try to do this I could move on X axis but at the same time the object remained motionless on Y axis.

It’s not MovePosition X only no.

So this is a Dynamic body if it has gravity; why are you using position specific movement then? Why not add a force or set the X velocity?

MovePosition is just a helper. It just sets the velocity to move to a position in a single simulation step. It also temporarily disables linear drag so it doesn’t get in the way. When it finishes, it restores whatever velocity there was along with the linear drag.

Position based movement is really mean for Kinematic movement. You can use it for Dynamic bodies but you have to be very careful. You want this and now you specifically only want positional movement in the X axis whilst maintaining dynamic movement in the Y axis.

It’s the wrong tool for the job. :slight_smile:

I am trying to move the object to where the finger is. So that is why I need to specific movement.

That doesn’t change what I said above. If you’re using gravity, you’re moving towards the point, not to exactly the point.

If you don’t follow what I said then it might be worth you using a more automated method such as the TargetJoint2D which you can configure to move something towards a point and calculate the appropriate forces for you. You can control how “hard” or “soft” this movement is too. Often the joint is used for “dragging” things. In the underlying physics engine, it’s actually known as the “mouse joint”.

There’s a demonstration I wrote in my PhysicsExamples2D repo many years ago. It’s actually used for all the dragging in all the scenes in it.

You can see it here:

I like the little happy faces on the objects :stuck_out_tongue:

1 Like