This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Im trying to make 2d enemy who will shoot toward right and left direction once player entered PlayerDetector area. I know how to instanciate projectile and make it move, but I have no idea how to write a code to set a direction.

Godot version ver3.3.3
in Engine by (82 points)

1 Answer

0 votes

just store the direction the enemy is moving at like this

dir = velocity.x/abs(velocity.x)
the abs return the positive value like |velocity.x|

the dir will then be either +1 or -1

and when instancing, remember to store the instance in a temp var so the properties can be changed.
var temp = bullet.instance() owner.add_child(temp)

now you can access the properties of the bullet and be like

temp.position = pos temp.velocity.x *= dir

the bullet needs to have something like:
position.x += speed *velocity.x

This is a lazy way to make it work and there are many other ways to do it though.

by (29 points)

Oh sorry to mention but enemy does not move. and bullet is kinematicbody2d.
bullet.direction = Vector2.Right kind of worked but not Left.

Then try

b.velocity = Vector2.RIGHT    
if Player.position.x < Enemy.position.x:
   b.velocity = Vector2.LEFT

or add two areas. that way, u may control if the player is too close that if it's under the eye line where the enemy shouldn't see it, it would stop shooting. It isn't needed though as the same effect can be gained by adding two collision shapes to the same area with the code above. I am late to reply but hope this helps in case.

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.