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)


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

#$ThrowShape.disabled = true
#$PickUpShape.disabled = false
if body is Enemy:
	gravity_scale = 5
elif body is Player:
elif body is Boss:
	linear_velocity -= Vector2(100, 100)
	if $Animation.flip_h:
		direction = -1
	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():
	yield($TimeoutFade, "animation_finished")
:bust_in_silhouette: Reply From: klaas

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