I want my enemy to turn to face the player but I can’t figure out the raycast thingy.
How do you think it should work?
Do you have any knowledge abouth linear math?
Nope, but I am trying to get my enemy to turn in a direction until it faces the player. Is raycast the correct thing in this instance? I’m not sure. My goal for the enemy is to make it turn to face the player, then move towards them and shoot bursts of bullets.
How would you know enemy is facing player or not?
my idea was with raycast, and the result would either be null, tree, or the player. If the result was the player, the enemy would stop turning.
So it should turn continuously, right?
Until the player is in front of it
Okay, then there are a lot of ways you can achieve that.
But imagine situation, when enemy face with player and player moves left a little. Will enemy turn again?
I was hoping, yes. Is there a way to do that?
Sure, it’s pretty easy.
But still, your enemy is kinda retard, if it start to turn when player moved cross it’s rotation.
Probably it should turn to face player, in any direction. And if player moves, find a closest way to face him again.
This is a little more complicated, but still, easy, if you know some linear math.
Ok so how do i go about that
I want you to read this article
I find it extremely useful.
When you will done, I’ll explain how to implement this on PC.
Ok, got that. Now how do I implement this with PlayCanvas?
Okay, you need to find a vector, which you should turn you enemy in order to face the player.
Lucky to you, there is a build-in function for this purpose. But I strongly recommend you to understand how it works.
var position = this.player.getPosition().clone(); this.entity.lookAt(position); // this.entity.rotateLocal(90,0 , 180); If your enemy is not looking forward by default.
Ok so I have it so it looks at the player. Do I use the rotate you mentioned, or what?
I don’t know exactly, depends on your implementation.
Now it always looks at player, okay. The next step is to make it turn.
You have to find a difference between enemy’s current direction and direction, when it sees player.
And now you have to use linear math from that article.
I’m sorry, but I wound’t tell you the straight way to do that. You have to solve it by yourself.
There is another useful article which describes how lookAt function works.
I looked at the game. It does everything right… except one thing. I want its gun to point at the player, so how do I make the computer overcorrect a little bit? Do I just add the position variable to a number?
Basically, yes. Add difference between gun’s and enemy’s directions.