1 year ago

#219326

test-img

dekYH

I don't want to let an object go out of the fan-shaped area

enter image description here

Like the attached image,

I don't want the red circle following the green mouse position to go out of the fan-shaped area.

So I wrote the code as below. However, it is not working properly. What's the problem?

    float GetDegree(Vector3 start, Vector3 end)
    {
        var dx = end.x - start.x;
        var dz = end.z - start.z;
        var degree = Mathf.Atan2(dx, dz) * Mathf.Rad2Deg;
                            
        return degree;
    }



    void update()
    {
        float range = 5;
        float centerAngle = 30;
        Vector3 centerPosition = new vector3 (0, 0, 0);
        Vector3 mousePosition = Input.mousePosition
    
        float R = Vector3.Distance(centerPosition, mousePosition);
        float T = GetDegree(centerPosition, mousePosition);
    
        float tmin =  Mathf.Abs(90f - (centerAngle) * 0.5f) * (T >= 0 ? 1F : -1F);
        float tmax = (T >= 0 ? centerAngle : -centerAngle) + tmin;
    
        float r = range;
    
        float R1 = Mathf.Min(R, r);
        float T1 = Mathf.Clamp(T, tmin, tmax);
    
        float rcost = R1 * Mathf.Cos(T1);
        float rsint = R1 * Mathf.Sin(T1);
    
        Vector3 result = new vector3 (rcost, 0f, rsint);
    }

c#

unity-game-engine

angle

degrees

0 Answers

Your Answer

Accepted video resources