Saturday, April 1, 2023
HomeGame Developmentphysics - Unity physics2d object repelling itself from static colliders

physics – Unity physics2d object repelling itself from static colliders


Ultimately emitting raycasts in all instructions and including a power in the wrong way of the closest hit labored fairly nicely

    void Radar()
    {
        float vary = 2f;
        float power = 15;
        
        float increment = 10f;
        Vector2 course = Vector2.up;
        Vector2 closestPoint = Vector2.zero;
        float minDistance = float.PositiveInfinity;
        bool discovered = false;
        
        for (int i = 0; i < 360/increment; i++)
        {
            RaycastHit2D hit =  Physics2D.Raycast(remodel.place, course, vary);
            if (hit.collider != null)
            {
                discovered = true;
                float distance = Vector2.Distance(hit.level, remodel.place);
                Debug.DrawLine(remodel.place, closestPoint);
                if (distance < minDistance)
                    closestPoint = hit.level;
                minDistance = Mathf.Min(distance, minDistance);
            }
            course = Hutl.rotatePointDegrees(course, increment);
        }

        if (discovered)
        {
            Debug.DrawLine(remodel.place, closestPoint);
            Vector2 forceDirection = ((Vector2) remodel.place - closestPoint).normalized;
            rb.AddForce(forceDirection * power);
        }
```

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments