#Mathf.Atan weird rounding to -90.0f

1 messages · Page 1 of 1 (latest)

amber bison
#
private float GetRayAngle(Ray ray)
    {
        RaycastHit hit = new RaycastHit();
        Physics.Raycast(ray, out hit, Mathf.Infinity);
        Vector3 vector = hit.point - Camera.main.transform.position;
        return Mathf.Atan(vector.y / vector.x) * Mathf.Rad2Deg;
    }

the result of the function ends up rounding to 90 degrees when the actual angle is 30 degrees from 90 degrees, but gives the actual value when Vector3 vector is within a certain range of distances from the center point

#

this happens when the angle should be 90 or 180

#

updated code to get to this:

private float GetRayAngle(Ray ray)
    {
        RaycastHit hit = new RaycastHit();
        Physics.Raycast(ray, out hit, Mathf.Infinity);
        Vector2 vector = new Vector2(hit.point.x - playerCamera.position.x, hit.point.y - playerCamera.position.y);
        return Mathf.Atan(vector.y / vector.x) * Mathf.Rad2Deg;
    }
lethal venture
amber bison
#

yes

#

i think

#

maybe?

#

let me check

lethal venture
amber bison
#

its not starting at the camera's position but its starting at that rectangle infront of the camera

#

i think the ui thing im using (Nova) is using cs Camera.ScreenPointToRay()

lethal venture
#

probably isn’t the issue then

lethal venture
amber bison
#

?

#

what do you mean

lethal venture
#

I might be assuming too much 😅

lethal venture
amber bison
#

im trying to get the angle relative to the center of the screen in order to tell which element im clicking because hit boxes are weird

#

currently im trying to find the angle

lethal venture
amber bison
#

the camera does not rotate

lethal venture
#

oh and it looks perfectly straight?

amber bison
#

yes

lethal venture
#

perfect

lethal venture
amber bison
#

when the angle is within a distance of 30 degrees to 90 degrees or 180 degrees, the cursor must be a certain distance from the center of the screen to give an accurate reading

#

else it will return 90 degrees

lethal venture
#

you also probably want to use Atan2 so you get the full range of angles and not just -90 to 90

amber bison
#

still happening

lethal venture
#

hmm

amber bison
#

k

lethal venture
amber bison
#

hit.point or the vector

lethal venture
amber bison
#

what does a raycast return if it misses?

lethal venture
amber bison
#

thats what it returns in those cases

lethal venture
amber bison
#

yeah

#

the ray is missing the ui somehow so i dont think the ui api im using is using Camera.ScreenPointToRay()

lethal venture
# amber bison

There’s a bit too much happening, are you trying to make it so that the arrow in the center rotates to face the edge of the circle if the mouse hits it?

amber bison
#

no im just trying to get the angle ignore everything else

lethal venture
#

hmm?

amber bison
#

what im doing is using the angle to tell what part of the ring i am clicking because i cant do custom ui click boxes

lethal venture
#

or maybe it’s a perspective thing, it’s too tricky to tell

lethal venture
# amber bison

Something you might want to take advantage of is that Physics.Raycast() returns a bool that is false if it misses something

amber bison
#

the debug.draw ray doesnt draw when the ray passes through

#

does it mean that misses

lethal venture
amber bison
#

i see

#

i might try to use Camera.ScreenPointToWorldSpace instead of the ray the ui gives me