0 votes

I am trying to rotate an animation based on the rigid body's collision point, but look_at doesn't seem to do anything. Here's my code:

class_name Sword
extends RigidBody2D

var direction = 1
var collision_angle = 0
var collision_pos
onready var player = get_parent().get_parent().get_parent()

func _on_RigidBody2D_body_entered(body):

set_collision_mask_bit(0, true) # after it hits something, enable collision with player so they can pick it up
set_collision_mask_bit(4, false) # prevent enemies from constantly colliding with sword
set_collision_layer_bit(5, false)

$ReturnTimer.start()

$ThrowShape.set_deferred("disable", true)
$PickUpShape.set_deferred("disable", false)

#$ThrowShape.disabled = true
#$PickUpShape.disabled = false
$Impact.play()
if body is Enemy:
    body.hurt(global_position)
    gravity_scale = 5
elif body is Player:
    body.pick_up("sword")
    queue_free()
elif body is Boss:
    linear_velocity -= Vector2(100, 100)
else:
    if $Animation.flip_h:
        direction = -1

    $Animation.look_at(collision_pos)
    $Animation.play("embed")
    sleeping = true

func _integrate_forces(state):
    if (state.get_contact_count() > 0):
        var position = state.get_contact_local_position(0)
        var angle = state.get_contact_local_normal(0).angle() 
        collision_angle = angle * 180 / PI
        collision_pos = to_local(position) 



func _on_ReturnTimer_timeout():
    $TimeoutFade.play("fade")
    yield($TimeoutFade, "animation_finished")
    player.pick_up("sword")
    queue_free()
Godot version v3.3.2.stable.official
in Engine by (22 points)

1 Answer

+1 vote
Best answer

Hi
since your node is a rigidbody, i suggest the lookat gets overwritten by the bodies physics. Am i right?
To modify a physics body transform you have to do it in the _integrate
forces(state) by modifying the transform in the PhysicsDirectBodyState provided to the function.

by (4,012 points)
selected by

Yes, it's working now, thank you. Didn't consider this would also have to be done in _integrate_forces(), but it seems to be the case. I should also add that the collision point should be kept global, otherwise the look_at() doesn't seem to rotate correctly.

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.