Look_at and Rotate causing endless spinning

Hi, im quite new into this Godot thing, im currently trying to do something that makes the enemy spin to face the player constantly, after looking for hours to a solution to this, ive seen that the most common line for this is the following:

func _physics_process(delta):
	look_at(player.global_position)
	rotate(PI/2)

This, however, causes the enemy sprite to spin endlessly without focusing on the player, i tried to fix this by just spinning the sprite 90° degrees but it doesnt stop spinning, its clear that somethings not right and i would like to know if theres something i can do to fix this.

If you all need some additional info, this scrip is attached to a CharacterBody2D node with just a sprite node.

whats the rotate(PI/2) for? that should be the reason its rotating endlessly

2 Likes

Ive seen it used a lot around this one code, though it could be what made the sprite rotate to follow the player since just using look_at doesnt do the work for some reason

Does look_at produce a warning or error?

Nope, although leaving it without Rotate doesnt make it spin around.

Does look_at on it’s own rotate the enemy at all? Even just once?

Nope :,(

Very interesting. look_at should alter a Node2D’s rotation, the next rotate(PI/2) line shouldn’t continously spin with look_at or any other rotation set.

Could you paste your entire script as it is?

The code that i posted is the entire script, however it is attached to a CharacterBody2D Node, should i change it to a Node2D for it to work?

CharacterBody2D inherits from Node2D, so that is ok. How is player defined? Could you try this same snippet under _process instead of _physics_process

The player node ( in my case, named “phage” since the original script has its name instead of player in the second line) is another CharacterBody2D Node defined with 4 child nodes, a collision, a timer dedicated to the cooldown for bullets, a marker that generates said bullets and the sprite.

imagen

as for the _process snippet, i tried it out but its still spun nonstop.

I mean, how is var player defined in the enemy script. Pasting the entire script would help in this case.

Ooooh gotcha, sorry.

Heres the script:

extends CharacterBody2D

@onready var phage: CharacterBody2D = $"../Wheel/Area2D/Phage"

func _physics_process(delta):
	look_at(phage.global_position)
	rotate(PI/2)

The look_at() should be enough to rotate your Node. See here my quick dirty demo showing that it in fact works. I made it so that it follows the mouse_position for simplicity.


obraz

extends Node2D
func _physics_process(delta):
	var position: Vector2 = get_viewport().get_camera_2d().get_global_mouse_position()
	look_at(position)

You can see that the code is not the problem. There has to be something else in your project that prevents the proper rotation. Would that be ok if you shared your project, e.g. via GitHub? This way we can debug and help you much easier. At the moment it’s like looking for a needle in a haystack, blindfolded.

1 Like

Of course, didnt use this before but i got the hang of it quickly:

I got it, told you it’s gonna be much faster and easier to debug with access to the project :slight_smile:

What you did wrong is that you moved only your Phage’s children to the outer ring, and left the Phage in the center.


You need to reset position of its children to (0, 0), and move only the Phage itself to this position (0, 404)

And your scanio.gd script doesn’t need this last line, as we told you from the beginning, this should be enough:

extends CharacterBody2D

@onready var phage: CharacterBody2D = $"../Wheel/Area2D/Phage"

func _process(delta):
	look_at(phage.global_position)
	#rotate(PI/2)

After you do these changes, the game should work as I showed in the video demo.

2 Likes

Well il be dammed, really didnt expect it to be that simple.

Thank you thank you soooo much!, you saved me a lot of stress with this :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.