0
\$\begingroup\$

I have a destination and 2 players.

I want the players to reach the destination and not overlap in the process.

I attached to each player a circle 2D collider and a rigidbody 2D.

enter image description here

Also this script:

using UnityEngine;
using System.Collections;

public class Agent : MonoBehaviour {

    public Transform destination;
    private Vector2 destPostion;
    private Vector2 playerPosition;

    public float rotation = 0;

    Vector2 velocity = Vector2.zero;

    public int maxForce = 5; //rate of acceleration
    public int maxSpeed = 4; //grid squares / second

    //private Rigidbody2D rb;

    void Start () {
        //rb = gameObject.GetComponent<Rigidbody2D>();
        destPostion.x = destination.position.x;
        destPostion.y = destination.position.y;
    }


    // Update is called once per frame
    void FixedUpdate () {
        Vector3 temp = transform.position;
        destPostion.x = destination.position.x;
        destPostion.y = destination.position.y;

        playerPosition.x = transform.position.x;
        playerPosition.y = transform.position.y;
        //Work out the force for our behaviour
        var seek = steeringBehaviourSeek();

        //Apply the force
        velocity = velocity + (seek * Time.deltaTime);

        //Cap speed as required
        var speed = velocity.magnitude;
        if (speed > maxSpeed) {
            velocity = velocity * (4 / speed);
        }

        //Calculate our new movement angle
        rotation = Mathf.Atan2(velocity.x, -velocity.y) * 180 / Mathf.PI;
        transform.rotation = Quaternion.Euler(0, 0, rotation);

        playerPosition = playerPosition + (velocity * (Time.deltaTime));

        temp.x = playerPosition.x;
        temp.y = playerPosition.y;
        transform.position = temp;
        //rb.MovePosition(temp);
    }

    private Vector2 steeringBehaviourSeek() {

        //Desired change of location
        Vector2 desired = destPostion - playerPosition;

        //Desired velocity (move there at maximum speed)
        desired = desired * (maxSpeed / desired.magnitude);

        //The velocity change we want
        var velocityChange = desired - velocity;

        //Convert to a force
        return velocityChange * (maxForce / maxSpeed);
    }
}

The problem is they start moving for a bit and they stop.

Also in the scene when I move them with the mouse they overlap. Any ideas?

\$\endgroup\$
1
\$\begingroup\$

Circle colliders aren't magic. They just let the physics engine detect and respond to collisions with appropriate impulses to change the trajectory of objects in the physics update step.

When you position an object with transform.position = ... you're saying "I do not care what the physics engine thinks, put the object EXACTLY HERE" - and the engine listens, and does exactly what you told it to do, even if that breaks the rules of physics collisions, because you told it you wanted to override whatever the result of the last physics step was. A circle collider won't intercept your transform commands and change them into something else.

If you care about physics behaviour like avoiding collisions, then you need to do one of two things:

  1. Move your objects with physics, by using the Rigidbody2D's forces, velocity, angular velocity, etc. That puts control back into the physics engine's hands to stop or deflect objects if a particular motion would cause them to penetrate.

  2. Do your own checking for collisions using raycasts or shapecasts, and back off if a planned position change would take you into a collision.

\$\endgroup\$
  • \$\begingroup\$ I have used rigidbody.MovePosition(), and they are still stuck.. \$\endgroup\$ – Gabriel Apr 3 '18 at 17:14
  • \$\begingroup\$ Right, because you're still giving it a position. "Place yourself EXACTLY HERE" you're telling it, not "Travel with this velocity and stop/bounce off if you hit something." If you tell the engine you want the object at a particular position, it won't second-guess you. It's your responsibility to choose a position that won't intersect, or move the object with physics velocity if you want the physics engine to resolve the trajectory using physics calculations. \$\endgroup\$ – DMGregory Apr 3 '18 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.