+1 vote

Hello,

I am attempting to update my sprite animation based on the mouse direction

My player script is as follows:

extends KinematicBody2D

const IdleSpeed = 10

export (int) var speed = 200
var velocity = Vector2()
var RayNode
var PlayerAnim
var anim = ""
var animnew = ""

func _ready():
    set_physics_process(true)
    RayNode = get_node("SpriteRotationRC")
    PlayerAnim = get_node("PlayerAnim")

func _physics_process(_delta):

    var look_vec = get_global_mouse_position()
    RayNode = look_at(look_vec)

    get_input()
    velocity = move_and_slide(velocity)

    if (velocity.length() > IdleSpeed*0.09):
        if (RayNode.get_rotation() == 180):
            anim = "Walk_U"
        if (RayNode.get_rotation() == 0):
            anim = "Walk_D"
        if (RayNode.get_rotation() == 270):
            anim = "Walk_L"
        if (RayNode.get_rotation() == 90):
            anim = "Walk_R"
    else:
        if (RayNode.get_rotation() == 180):
            anim = "Idle_U"
        if (RayNode.get_rotation() == 0):
            anim = "Idle_D"
        if (RayNode.get_rotation() == 270):
            anim = "Idle_L"
        if (RayNode.get_rotation() == 90):
            anim = "Idle_R"

    if anim != animnew:
        animnew = anim
        PlayerAnim.play(anim)


func get_input():
    velocity = Vector2()
    if Input.is_action_pressed("PlayerRight"):
        velocity.x += 1
    if Input.is_action_pressed("PlayerLeft"):
        velocity.x -= 1
    if Input.is_action_pressed("PlayerDown"):
        velocity.y += 1
    if Input.is_action_pressed("PlayerUp"):
        velocity.y -= 1
    velocity = velocity.normalized() * speed

The raycast2d is attached to a RemoteTransform2D and does appear to rotate to the mouse when I attach a sprite as a child to it however I don't see any animations changing (although the walking animation does work just not including the directions)

asked May 6 in Engine by Leightonne (21 points)

1 Answer

0 votes

I gave up on this method and went with the following solution.

extends KinematicBody2D

const IdleSpeed = 10

export (int) var speed = 200
var velocity = Vector2()

var PlayerAnim
var anim = ""
var animnew = ""

var MousePosition
var RotationAngle

func _ready():
    set_physics_process(true)
    PlayerAnim = get_node("PlayerAnim")

func _physics_process(_delta):
    get_input()
    velocity = move_and_slide(velocity)

    MousePosition = get_global_mouse_position()
    RotationAngle = get_angle_to(MousePosition)

    print(RotationAngle)

    if (velocity.length() > IdleSpeed*0.09):
        if RotationAngle >= -2 and RotationAngle <= -1:
            anim = "Walk_U"
        if RotationAngle >= -1 and RotationAngle <= -0.25:
            anim = "Walk_UR"
        if RotationAngle >= -0.25 and RotationAngle <= 0.25:
            anim = "Walk_R"
        if RotationAngle >= 0.25 and RotationAngle <= 1:
            anim = "Walk_DR"
        if RotationAngle >= 1 and RotationAngle <= 2:
            anim = "Walk_D"
        if RotationAngle >= 2 and RotationAngle <= 2.9:
            anim = "Walk_DL"
        if RotationAngle >= 2.9 or RotationAngle <= -2.9:
            anim = "Walk_L"
        if RotationAngle >= -2.9 and RotationAngle <= -2:
            anim = "Walk_UL"
    else:
        if RotationAngle >= -2 and RotationAngle <= -1:
            anim = "Idle_U"
        if RotationAngle >= -1 and RotationAngle <= -0.25:
            anim = "Idle_UR"
        if RotationAngle >= -0.25 and RotationAngle <= 0.25:
            anim = "Idle_R"
        if RotationAngle >= 0.25 and RotationAngle <= 1:
            anim = "Idle_DR"
        if RotationAngle >= 1 and RotationAngle <= 2:
            anim = "Idle_D"
        if RotationAngle >= 2 and RotationAngle <= 2.9:
            anim = "Idle_DL"
        if RotationAngle >= 2.9 or RotationAngle <= -2.9:
            anim = "Idle_L"
        if RotationAngle >= -2.9 and RotationAngle <= -2:
            anim = "Idle_UL"

    if anim != animnew:
        animnew = anim
        PlayerAnim.play(anim)


func get_input():
    velocity = Vector2()
    if Input.is_action_pressed("PlayerRight"):
        velocity.x += 1
    if Input.is_action_pressed("PlayerLeft"):
        velocity.x -= 1
    if Input.is_action_pressed("PlayerDown"):
        velocity.y += 1
    if Input.is_action_pressed("PlayerUp"):
        velocity.y -= 1
    velocity = velocity.normalized() * speed
answered May 7 by Leightonne (21 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.