- Home /
How to anticipate the Vector3.forward of the enemy?
Hi,
i would like my character to anticipate the direction of the enemy, in order to catch him. But my character looks too slow, and rotate just after the enemy passes him. So i would like the character to anticipate the next step of the enemy, with Vector.forward * 5 (in this example), but he does not seem to understand what i am trying to do, the z axis does not rotate fast enough so that the character can catch the enemy. Would you know why?
Here is the code :
//rotate the character every frame
if(enemyFound == true){
//enemy to follow
var newTransform : Vector3 = enemyToFollow.transform.position;
newTransform += newTransform.forward*5*Time.deltaTime; //anticipate a bit, does not work
directionToClosestEnemy = newTransform - transform.position;
tempRot = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(directionToClosestEnemy),2*Time.deltaTime);
}
//make him move
if(enemyFound == true){
targetVelocity = enemyToFollow.transform.position - transform.position;
targetVelocity = targetVelocity.normalized; // Is now speed 1, going correct way
targetVelocity = targetVelocity * MOVE_SPEED;
var velocityChange = (targetVelocity - velocity);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.Force);
}
Thanks
Edit: do you mean like this ?
var newTransform : Vector3 = enemyToFollow.transform.position;
newTransform += enemyToFollow.transform.forward * 100 * Time.deltaTime;
directionToClosestEnemy = newTransform - transform.position;
tempRot = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(directionToClosestEnemy),2*Time.deltaTime);
Answer by Piflik · Sep 05, 2012 at 08:50 PM
The reason why it doesn't work is that you don't add the enemy's forward vector, but the Z-component of its position. You need to replace 'newTransform.forward' with 'enemyToFollow.transform.forward'.
@Philipp Jutzi thanks, it does not seem to respond to my setting, i edited my previous post, would you know why it does this? The character still rotates after the enemy, ins$$anonymous$$d of anticipating the direction of the enemy.
It doesn't work, because you don't even use the stuff you are calculating. You are just applying a force that is calculated from the current position of the enemy.
Try this:
private var multiplier : int = 100; // how much do you want the character to lead the enemy
if(enemyFound) {
var enemyPos : Vector3 = enemyToFollow.transform.position + enemyToFollow.transform.forward * multiplier; //no Time.deltaTime...this is a fixed position in front of the enemy, independent of the framerate
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((enemyPos - transform.position).normalized));
velocityChange = ((enemyPos - transform.position).normalized * $$anonymous$$OVE_SPEED) - velocity;
velocityChange.y = 0;
rigidbody.AddForce(velocityChange);
}
@Piflik thanks Piflik! you 're right, i needed to use enemyPos for the position too, not only the rotation. Thanks a lot, it works great! (edit: don't know how to do a +1 for your answer, but thanks a lot)
Your answer
![](https://koobas.hobune.stream/wayback/20220613080655im_/https://answers.unity.com/themes/thub/images/avi.jpg)