#how to treat a vector3 as a vector2 while using distance_to

61 messages · Page 1 of 1 (latest)

wet heron
#

i'm making a 3d stealth game. i have an enemy with a raycast always pointing towards the player's head. i want to compare the raycast's rotation with the enemy's overall rotation to determine if the player is within the enemy's view cone. the issue is, i want to ignore the y axis, i don't want the calculation to be affected by height. if i do distance_to with the vector3's, it will factor in height. what's an efficient way to avoid this?

unreal ginkgo
#

You can try to set y axis from both vector to 0 then calculate the distance

wet heron
#

and just change it back afterwards?

unreal ginkgo
#

So you have a view cone and a raycast that always point to player, they are different nodes?

normal pine
#

Though if you just care about distance, you can easily calculate that yourself without using the vector function

wet heron
#

i think i was wrong saying i don't care about the y rotation, i keep getting the coordinates mixed up

#

i only want to compare the y

wet heron
#

this works

#

it just seems inefficient to have these extra vectors

unreal ginkgo
#

global_rotation.distance_to(view_turret.global_rotation) looks wrong

wet heron
#

why

#

im trying to compare the angle of the enemy's root node with the angle of a child it has that points towards the player

#

i'm not using that specific code anymore regardless

#

but it did what i expected

unreal ginkgo
#

I guess distance_to should be used to measure position distance, so I always use on position or global_position, not rotation

wet heron
#

link to vector3 distance_to method

unreal ginkgo
#

I don't know how to explain, but looks weird to use distance_to when the Vector3 is a rotation

#

It feels like you should to use angle_to()

wet heron
#

seems to do the same thing

#

i tried it

#

just wider view angle

#

nvm, the number i enter doesn't seem to affect it

#

is there any benefit to using this over the other one

#

since the other one worked

unreal ginkgo
#

I don't think so

wet heron
#

i did figure out that one of my extra vectors was redundant since the enemy only rotates on the y axis, i'm going with this

#

still feels excessive to make a whole other vector3 just to omit something

normal pine
#

hold on..

wet heron
#

im mostly concerned about it since a bunch of different objects will be running this every frame

normal pine
#

Can't you just do
abs(view_turret.global_rotation.y - global_rotation.y)

#

Since you just want to know the difference in y rotation

wet heron
#

yeah that works here

#

but i have sort of an instinct not to subtract rotations like that because they can loop around, like in gamemaker i would get issues if one was 350 and another was 10, they'd think they're 340 apart and not 20 apart

#

isn't that going to be an issue?

#

(i'm still pretty new to godot)

#

i was excited about vectors because it seemed like it would avoid that stuff

normal pine
#

That can be an issue (I've encountered it before), but you'd get the same problem using .distance_to()

#

There are workarounds

#

Or you could try to explore Quaternions (They're worth it)

wet heron
#

you're saying that using distance_to on a vector will have that issue? i thought one of the most important purposes of vectors and that method is to get around that

normal pine
#

Quaternions are what avoids all the common pitfalls.

wet heron
wet heron
normal pine
unreal ginkgo
wet heron
#

it's weird that distance_to even exists, i feel like it should never be used if it has this issue

#

i tested it and just can't believe that it happens

#

like i can't imagine a situation where i'd be ok with using that method

normal pine
#

It's meant to be used on positions.

#

For positions, it's perfect

#

It really isn't meant for rotations.

#

Vectors are mostly used for positions, Quaternions are used for rotations internally, the Vector rotations are just there for the benefit of the developer, to give a simple way to interact with rotations, which works for a good amount of use cases.

wet heron
#

ic

wet heron