0 votes

Hello! I can't speak English well, so I hope you understand that I use Google Translator.

There are clockwise and counterclockwise ways to find the perpendicular vector, but I want to make the enemy push vertically differently as shown in the picture. In this case, should I compare the enemy's position with the player's position and get all of them separately? For example, using an if statement. help me from your knowledge, thank you.

This is my first question, so I don't know how to upload a picture. Sorry :)


Godot version 3.2.3
in Engine by (75 points)

1 Answer

+1 vote
Best answer

So if I needed to do this, I would try doing it this way:
1) get the enemy's position and the player's position
subtract them like this: var vector_from_player_to_enemy = enemy_position - player_position, there may be a built-in way to do this, the idea is to get the vector that points from the player to the enemy
2) get the angle from the player's direction to this vector player_direction.angle_to(vector_from_player_to_enemy)
the sign (+ or -) of the angle will tell if the enemy is on the left or right side of the player
I am not sure that angle_to can actually return negative angles, but -0.6 is the same (in radians) as 2*PI-0.6, so you may need to check if it is bigger than PI (or convert it to degrees of course if you like that better: var angle_deg = rad2deg(angle_rad)), you should take a look at the values it returns (print() for example) and understand them so you can check for them well
3) you can then rotate the player's direction (movement or velocity?) vector by + or - 90° (which are +-PI/2 radians)
note that the engine uses radians for the angles, so you need radians in here too: var enemy_direction = player_direction.rotated(PI/2)

by (288 points)
selected by

It was about adding a knockback, but it was smooth. Thank you!

var distance: Vector2 = (owner.position - area.owner.position).normalized()
var degree = excuse_vector.angle_to(distance)

    if degree / 90 >= 0:
        knockback_vector = Vector2(excuse_vector.y, -excuse_vector.x)
        knockback_vector = Vector2(-excuse_vector.y, excuse_vector.x)
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.