Home > front end >  Vector3.MoveTowards not working with collisions?
Vector3.MoveTowards not working with collisions?

Time:09-17

I am trying to make smooth plane movement that follows the cursor.

This is the code that does so:

void Update()
    {
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = 0;
        Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);

        mousePos.x = mousePos.x - objectPos.x;
        mousePos.y = mousePos.y - objectPos.y;

        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle   rotationOffset));

        Vector3 targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        targetPos.z = 0;
        transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);

        if (transform.position == targetPos)
        {
            Die();
        }
    }
    public void OnCollisionEnter2D(Collision2D col)
    {
        Debug.Log("hit");
    }

This works well by its self but I have run into an issue where it doesn't detect collision. Ive never used Vector3.MoveTowards with movement before but I'm guessing that's the problem. If anyone had any suggestions it would be very appreciated.

CodePudding user response:

Relating to Armin's comment, transform.position==targetPos will virtually never return true. That is just because of the large amount of precision in floats and inherent problems with floating point comparison.

A simple solution would be to check if the distance is within some delta. I recommend using square magnitude of distance for performance reasons. Example:

const float squareCollisionDistance = 4f;//actual collision distance will be 2
var offsetVector = targetPos-transform.position;
var squareDistance = offsetVector.sqrMagnitude;
if (squareDistance < squareCollisionDistance)
    {
        Die();
    }

Something else to look into is RigidBodies I think they have a collision event that you can listen to. Collision detection can be really tricky business and it is worthwhile to use Unity's built in features when you can.

CodePudding user response:

I'm gonna assume you're using if (transform.position == targetPos) to check if the objects are colliding and or near each other. The problem is, these vectors will only in a very very rare instance actually be equal, because the internal values go far into decimals.

You can try replacing this with with Vector3.Distance such as

float collisionDistance = 0.1f; // here you can fine tune the distance at which they collide

if (Vector3.Distance(transform.position, targetPos) < collisionDistance)
    {
        Die(); // love this function name
    }

The following collision function has no errors

public void OnCollisionEnter2D(Collision2D col)
{
    Debug.Log("hit");
}

So I think your problem might be that the objects do not have a rigidbody. At least one of the objects must have a rigidbody for this function to fire. Like I mentioned, alternatively you can make them both triggers.

  • Related