Can someone help with aiming code? (top down 2D)

Godot Version

4.2.1

Question

mofatesh aim
I want when player moves the crouser the animation state changes (im using animation tree) based on the crusor placement. here is the code:


@onready var state_machin = animationtree["parameters/playback"]

enum {IDLE, RUN, GUN}
var state=IDLE
var blend_pos:Vector2=Vector2.ZERO
var blend_pos_path=[
	"parameters/idle/idle_bs2d/blend_position",
	"parameters/run/run_bs2d/blend_position" ,
	"parameters/gun/gun_bs2d/blend_position"
]
var anime_state_key=[
	"idle",
	"run",
	"gun"
]
func move(delta):
	var input_vector
	if aim==true:
		state=GUN


	if Input.is_action_just_pressed("aim") and gun and on_stairs==false:
		aim=true
		state=IDLE
		velocity=Vector2.ZERO
	if Input.is_action_just_released("aim"):
		state=IDLE
		aim=false
		
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and aim==true and canShoot:
		var dir= get_global_mouse_position()-position
		shoot.emit(position,dir)
		canShoot=false
		$Timer.start()

func _physics_process(delta):
	move(delta)
	animate()


func animate()->void:
	state_machin.travel(anime_state_key[state])
	animationtree.set(blend_pos_path[state],blend_pos)

Just use the _input()-method:

func _input(event: InputEvent) -> void:
    if event is InputEventMouseMotion:
       var angle: float = rad_to_deg(global_position.angle_to_point(event.global_position))
        # change position in animationtree

ok but how should i change? I dont know what codes use for it, based on the animation tree… based on the codes where should i put this… mate im a noob

This method needs to be in the player-script. Do you have a blend-space2d in the animation-tree?

1 Like

yes I do

My problem is i dont know how to change the animation stats via code

and the codes are up there if you need checking

1 Like

To set the value you would need to run something like this:

animationTree.set("parameters/gun_bs2d/blend_position", input_vector)

and together with the mouse-movement it would look like this (havent tested it, might be wrong):

func _input(event: InputEvent) -> void:
    if event is InputEventMouseMotion:
        var direction_vector: Vector2 = (event.global_position - global_position).normalized()
        animationTree.set("parameters/gun_bs2d/blend_position", direction_vector)
1 Like

its not working, it doesnt crash or anything, just there is nothing happening

Your path has to be this. You also need a reference to your animationtree:

 @onready var animationTree: AnimationTree = $AnimationTree

func _input(event: InputEvent) -> void:
    if event is InputEventMouseMotion:
        var direction_vector: Vector2 = (event.global_position - global_position).normalized()
        animationTree.set(blen_pos_path[state], direction_vector)

no changes again T_T

You never change the state from “IDLE” to anything else. You have to set the “state”-variable according to current state

No the state changes, he gets the gun, but he doesnt rotate with cursor:

func move(delta):
	var input_vector
	if aim==true:
		state=GUN

I forgot to put it in the codes up there mb

change the input-method to this

func _input(event: InputEvent) -> void:
    if event is InputEventMouseMotion:
        blend_pos: Vector2 = (event.global_position - global_position).normalized()

it gives me an error"expected end of statement after expression, found “:” instead"

sorry remove the “: Vector2”

1 Like

thank you so much

2 Likes

may I ask your name? I want to credit you in the credits… this problem has taken so much time and this is my way to say thank you

This is not neccessary :sweat_smile:. If you insist just put HerrSpaten

1 Like

I will

1 Like

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