Hi guys,
I am using C# to create a game with Godot.
I am creating an enemy who has to jump a determined distance up and, once there, he has to go directly to the Player to give the Player a kick in his face. I have tried to do it, setting a Ray 2D (TheRay) in the enemy’s base and using IsColliding() to determine if the enemy is Colliding with the Floor (the tiles).
Later I use Move Toward to jump until the a point (the TopPosition) and when the Enemy has reached the TopPoint (Position.y<=TopPosition.y) I use another MoveToward to move the Enemy to the Player (to give him the kick). The problem is that the enemy
jump but doesn't have a linear trajectory to the Player but the enemy travel iterating (as a fly that is going to some food) and I don’t know why. Do you know what could be the error?
This is part of the code of the Enemy script:
public override void _PhysicsProcess(float delta)
{
if(HealthOfTheEnemy>0){
velocity.y += Gravity * delta;
MoveAndSlide(velocity,Vector2.Up);
velocity.y += Gravity * delta;
MoveAndSlide(velocity,Vector2.Up);
if(PlayerIsNear){ //the PlayerIsNear variable is a boolean that become activated with a Signal
if(ThePlayer.GlobalPosition.x > this.GlobalPosition.x){
TheSprite.FlipH = false;
} else{
TheSprite.FlipH = true;
}
if(TheRay.IsColliding()){
TheTargetPosition = ThePlayer.GlobalPosition;
TopPosition = new Vector2(Position.x,Position.y -1000);
Position = Position.MoveToward(TopPosition,5);
}
if(!(TheRay.IsColliding())){
if(Position.y > TopPosition.y){
GlobalPosition =GlobalPosition.MoveToward(TopPosition,5);
GD.Print("it is going up");
}
if(Position.y<=TopPosition.y){
if(TheTargetPosition.x > GlobalPosition.x){
Position = Position.MoveToward(TheTargetPosition, 5);
}
}