The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

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)


$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")
Godot version v3.3.2.stable.official
in Engine by (22 points)

1 Answer

+1 vote
Best answer

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,088 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 Frequently asked questions and 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 [email protected] with your username.