1
\$\begingroup\$

How do I get my character to collide with other objects while moving to a position?

All I've managed to do is change the position, it only collides if the character is not moving, it's like he is moving behind.

void Update () {

    foreach(Touch touch in Input.touches)
    {

        if (touch.phase == TouchPhase.Began) 
        {
            worldCoordinates = Camera.main.ScreenToWorldPoint(touch.position);
            posi.x = worldCoordinates.x;
            posi.y = worldCoordinates.y;
            //transform.position = Vector3.MoveTowards(transform.position, posi, speed);
            //transform.Translate(0,32.4,0);
            //rb.MovePosition(posi);
        }
    }
} 
\$\endgroup\$
2
  • \$\begingroup\$ perhaps objects have different Z positions? \$\endgroup\$ Commented Jul 21, 2016 at 8:48
  • \$\begingroup\$ For moving to a position you can use lerp method. Then use OnCollisionEnter2D() to check collisions. \$\endgroup\$ Commented Jul 21, 2016 at 9:05

2 Answers 2

1
\$\begingroup\$

First of all, all your physics related stuff should be done in FixedUpdate method and not in Update method.

Update is called every frame, while FixedUpdate is called at fixed interval. When you ask Physics movement computation in FixedUpdate Unity will predict the new position in Update until it'll be able to compute the actual position in the next FixedUpdate (ie: it explains why fast moving objects are usually not stopped by walls. Because the FixedUpdate happens too soon or too late to compute the collision).

Also, if you want to move a GameObject that has a RigidBody attached don't move it using its Transform, do it using its RigidBody.

So instead of setting transform.position use directly rb.MovePosition. This will teleport your gameObject from its current position to the desired position every FixedUpdate. If it's not smooth enough, you can Lerp between the two positions in the Update method.

\$\endgroup\$
0
\$\begingroup\$

In addition to @lvictorino answer, maybe what you're looking for is in fact the AddForce method.

I'm not exactly familiar on how it could work with Unity, but roughly, what you could do is

// Rough pseudocode
difference = touchPosition -  objectPosition

distance = difference.lenght
direction = difference.normalize

radiusForMaxForce = 50 // tweak this value, above that distance maxForce will be used as-is, 
                       // otherwise it will be scaled down
maxForce = 25 // tweak this value according to your needs

if distance > radiusForMaxForce
  distance = radiusForMaxForce

forceRatio = distance / radiusForMaxForce

thrust = forceRatio * maxForce
forceVector = direction * thrust

rb.AddForce(forceVector)

If your target game object encounters other rigid body, the physics simulation should be respected: free bodies will be pushed around, and there is not going to be inter-penetration.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.