look_at not working in AnimatedSprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MadWorg

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()
:bust_in_silhouette: Reply From: klaas

Hi
since your node is a rigidbody, i suggest the look_at 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.

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.

MadWorg | 2021-08-16 12:11