#Can't make the gun point where I want

33 messages · Page 1 of 1 (latest)

hardy tusk
#

I'm trying to make this KinematicBody point where the mouse is but it always rotates in the opposite direction at inconsistent magnitudes and speeds.

I'm really not sure what I'm missing.

The script is attached to the "Gun" as opposed to the player, because the player isn't supposed to move at all. Only thing that moves is the gun.

Truthfully, the Player should only be able to aim in the 180 degree range above it, but I would like to learn how to implement this on a full 360 degree scale just for education's sake.

#

This is the code I was using. Ignore the comments. I was playing around with a bunch of different things trying to fix it.
Most of the code that gives them context has been deleted.

ocean steppe
#

The simplest way I've found to get something to face the mouse is with this line:

rotation += get_local_mouse_position().angle()
hardy tusk
#

I'm about to try it

#

It still doesn't quite line up but it does rotate when I move the mouse

#

Maybe there's some base alignment I need first?

hardy tusk
ocean steppe
#

You may need some base alignment, yeah. Remember that an angle of 0 means pointing to the right

hardy tusk
#

.... That may be the source of my problems

#

I had no idea about that

#

Now I need to figure out how to realign

#

Sorry I'm tired I gotta sleep. But I'll let you know how it goes

hardy tusk
#

Okay, I'm back. I looked at my alignments more closely and I think everything is fine tbh.

I suspect the issue is that the mouse_position.angle() does not convert directly into transform.rotation()

But when I tried converting from radians to degrees that also did nothing

#

Unless I'm missing something else

#

This never gave me a value close to 360 even when I moved all around the screen once

#

So it seems like the return type of map.angle() doesn't convert into the transform.rotation setting. But I'm not sure how to fix that

#

mouse_local_position seems to return Vectors? Or something that looks like Vectors. So I assume with that also the values aren't matching up, which is what is creating my misalignment

ocean steppe
#

Yes, the mouse position is returned as a vector.

radiant igloo
#

Generally you should avoid using angles for this stuff, as they have a tendency to do weird things around the 360 mark

hardy tusk
#

Ah... so what do I do then?

I've never tried to manipulate rotation in Godot before

ocean steppe
#

The problem with global mouse position is that it's not relative to the node, but to the 0,0 - which unless you have a camera, is always exactly the top left, limiting you to the 0 to 90 degree range

radiant igloo
#

I have two solutions in mind

#

either you can manually make sure the angle is within a reasonable range, something like this (psuedocode since I don't use gdscript)

var rotation = getAngleBetween(position, mousePosition);
if (rotation > 180) {
  rotation -= 360;
}```
#

using a range between -180 and 180 instead of 0 and 360 might help solve this specific issue

#

the other solution would be to interpolate between the current look position and the mouse's look position

var newPosition = basis.z.slerp(mousePosition.normalized());
basis.z = newPosition;

something like that

hardy tusk
ocean steppe
#

The important thing about local mouse position is that it's also relative to the current rotation of the node

#

So a local angle of 0 means you're facing the mouse

#

Which is why my solution has been to modify the current rotation by the current angle towards the mouse - the difference between them.

radiant igloo
#

getting the angle between the mouse position and any given position in global space would just be something like js atan2(mousePosition.x - position.x, mousePosition.y - position.y);

hardy tusk
#

This is the setup rn

hardy tusk
#

I have solved this. It needed an adjustment of 4.7 radians and the angle_to_point() method

#

How do I close this?