Trying to create a hitbox/object to track mouse position at all times

Godot Version v4.2.1

I’ve completed the tutorial on creating your first 2d Godot game, and wanted to make a variance where your mouse cursor is the player, wherein it would detect collisions and exist in the play area. I’ve looked every but I still haven’t found any solution to what is basically setting the player/hitbox position to the mouse cursors position. This is my first time using GScript without the guide so I’ve gotten a little lost sorry. Ty for any help it’s much appreciated <3

I would recommend you start by figuring out how to get something to follow the mouse. I’m not talking about custom cursors, but using actual physics impulses to move an object toward the mouse.

Since you’re talking about physics (at least I think you are) you can’t just set the position of the physics object directly. You might want to look into things like:

move_and_collide()

or

applying impulses and forces to a RigidBody2D, depending on the actual mechanics you’re talking about.

If you’re not talking about actual physics and you’re just looking to detect whether the mouse is within an Area2D or other object (like a button), the above would not apply.

Either way, try breaking the problem down into smaller chunks. If you want the mouse to be the player, maybe just focus on how to move a sprite toward the mouse and go from there. Take manageable bites! Good luck! We’ll be here if/when you have other questions!

2 Likes

Thank you for the help !
I think its kind of a problem of
I wouldn’t exactly need a sprite or player character over the cursor, as much as like -
basically needing to know whenever the cursor in the 2d area collides with another hitbox
Just kind of attaching a hitbox to the mouse.
Sorry I know it sounds a little weird but seriously thank you so much ! <3

Did you figure out how to do it?
If not, I could provide you with some of the ideas I would implement to make the collision work

1 Like

not yet, I’ve been trying to read up on GDscript syntax to find processes that can do what I need. Any help is much appreciated as I still haven’t found a solution, thank you.

Well, alright.
You want the mouse to be the player, then you should implement a script that lets the program know the mouse position.
You could try:
Var direction:
vector2=Vector2(get_global_mouse_position)

Now you need to put this in a function to look if this works.
You could try:
Func _process(_delta):

print(direction)

This way, you print the current position of your mouse.
If this works properly, you can implement other things into the function.
Just a disclaimer, I am not a 100% sure on how to do this, but this would definitely be the approach i would take. If this doesn’t work, I would be glad to help.
Have a great day

1 Like

Thank you,
it didn’t work and produced some errors.
I’m not quite sure what they meant, but here is what’s written:

extends CharacterBody2D

var direction:
	vector2 = Vector2(get_global_mouse_position)


func _process(_delta):
	print(direction)

and these are the errors they produced:
Line 4:Expected “get” or “set” for property declaration.
Line 4:Expected end of statement after property declaration, found “=” instead.
Line 4:Expected end of indented block for property.
Line 7:Expected end of file.

Thank you again so much for the help. This was supposed to be an easy first game assignment but apparently finding the mouse was too hard sorry.

I’m not sure if this is what you wanted but as far as I understood it you just want the mouse cursor to have a hitbox that can interact with stuff, right?

I just tested a simple bit of code with a CharacterBody2D that has a sprite (optional) and a CollisionShape2D.

extends CharacterBody2D

func _process(_delta):
	global_position = get_global_mouse_position()

If you move the mouse, the sprite and collision shape move.
I put it in a scene with an Area2D, that prints a statement if a body enters and it works.

1 Like

The errors are mostly because the syntax isn’t correct.

extends CharacterBody2D

var direction:Vector2 = Vector2(get_global_mouse_position()) 

func _process(_delta):
	print(direction)

This version is executable, but only prints “(0, 0)”

1 Like

Sorry, bit this isnt quite what I meant.
Try this:

extends CharacterBody2D

var direction: Vector2 = Vector2(get_global_mouse_position)

This is supposed to be one line! Just one line, sorry for that, my bad.

Do you understand what I’m saying?
This all is one line, but looks like 2 because there is not enough space here.

This will eliminate all errors(at least it should). This should be a good start to see if the var is working properly.
Any questions left? If so, I’d be glad to know

1 Like

ok thank you everyone so much ! acenixton’s code worked and it tracks perfectly with just slight delays from processing ofc :3 which is okay for my game ^^
Thank y’all for all the help !
in retrospect the simple code should have felt inherently obvious but, such is coding :​p

1 Like

No problem, hope sorry for the code-wroting issue in my comments

1 Like

ur good :​p
it works now :3

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.