0 votes

In my game i have an enemy that starts at the top and moves downward. My plan was for it to shoot out a raycast and hit an invisible collision shape behind the player that is part of the scene, and if it hits the wall instead of the player, compare the x value of the collision to the position of the player (the difference between the height of the invisible wall and the player collision is small so it wouldn't make much of a difference) and rotate the ship by a little bit to the left or right depending on whether the player is to the left or right of that collision (or if it is hitting the player, go towards the player). This way, the ship would have a little bit of angular velocity so the player has a chance to dodge it since the player can only move left and right. The only problem is that i don't know how to get the player's position. I tried i signal but i can't figure out how to make it work. Does anyone know the best way to do this? I don't want to do tree navigation, so anyone have any other idea that will work?

I want the code to look about like this:

func _process(delta):

    if $Raycast.get_collider().get_name() == 'Back':
        if $Raycast.get_collision_point().x < PlayerPosition.x:
            set_rotation_degree(get_rotation_degree() + rotatespeed)
        else:
            set_rotation_degree(get_rotation_degree() - rotatespeed)

Side question but also important, it is telling me that that setrotationdegree isn't how you set the rotation degree. What is the set function for rotation?

edit: i figured out the rotate() function, but how do i make the ship move in the direction it is facing?

in Engine by (243 points)
edited by

1 Answer

0 votes

The method I used in my own game is to assign the node of the player to a variable so that I always have access to it and its properties.
onready var player = get_node('/root').find_node(str)
Where str is the name of the player node.

by (3,892 points)

Well if that's the only way to do it i guess that's fine. Thank you! Nice to know about the find_node function at least. I'd just rather use something like signals to do it since that means i am not dependent on the tree so i can test it in different environments. But it works, so whatever.

Signals are sort of dependent on the tree, since you need both nodes at some point in order to wire up a signal :)

Another way to do this is to set a global variable with the player or add the player to a "player" group that only ever has one node in it. Those are both sort of sketchy approaches, though, so best not to use that sort of thinking too often.

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.