- Home /
Ai dodge bullets
I am making a geometry wars clone in 3d. I am now working on the green enemy which dodges the players bullets. https://www.youtube.com/watch?v=OxtZUURTHzs
I have a system that detects when a bullet comes close. It then calls the evadeBullet() function. The problem I have with this, is that it doesn't work as I want to. I want the enemy to sidestep when the bullet comes close. I have no idea how to do that.
With the code below, if the enemy comes from above the code works but when the enemy comes from the right the enemy runs right into the bullet. I could really use some help to get the same behaviour as the original geometry wars.
public void evadeBullet(GameObject bullet)
{
Vector3 moveDirection = bullet.transform.position - this.transform.position;
moveDirection *= -1;
float step = evadeSpeed * Time.deltaTime;
//Debug.Log(moveDirection);
transform.position = Vector3.MoveTowards(this.transform.position, moveDirection, step);
}
Answer by stormAster720 · Oct 11, 2018 at 10:29 PM
@Delthrox i think, you should use transform.RotateArround(moveDirection) or if you are using onTriggerEnter to detect bullets, you can use transform.RotateArround(other.transform) sorry for my bad english, it is not my native languaje
Answer by jasperbarg · Aug 15, 2019 at 07:54 PM
@Delthrox I happened to come across your problem trying to find a solution myself. I managed to fix the problem by having putting an empty gameobject (Right or Left) besides the projectile gameobject. Whenever you want the AI to sidestep just calculate the direction form the projectile to the empty gameobject and send that to the AI you want to evade, since that direction is the direction you want the AI to move in. Sorry in advance if the code is a bit messy. The following code is located on the bullet gameobject:
private void InPath()
{
if (EnemyAI != null)
EnemyAI.inBulletPath = false;
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.SphereCast(ray, 0.5f, out hit))
{
if (hit.transform.tag == "Enemy")
{
EnemyAI = hit.transform.GetComponentInParent<EnemyAI>();
EnemyAI.inBulletPath = true;
if (rayHitTarget)
{
Vector3 ghostPosition = RightOrLeft();
EnemyAI.evadeDirection = new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(ghostPosition.x, 0, ghostPosition.z);
rayHitTarget = false;
}
}
else
rayHitTarget = true;
}
}
private Vector3 RightOrLeft()
{
if (Random.Range(0, 2) < 1)
return Right.position;
else
return Left.position;
}
Then in the AI script just call the methode whenever the AI is in the path of the bullet. The two pieces of code a both located on the AI gameobject
private void EvadeBullet()
{
Entity.Move(evadeDirection.normalized);
}
Entity.Move Basicly handles the movement and rotation of the gameobject.
//verander acceleration
if(direction.magnitude > 0)
{
movementAcceleration += movementAccelerationFactor;
lastDirection = direction;
}
else
{
movementAcceleration -= movementAccelerationFactor;
}
//clamp acceleration tussen 0 en 1
movementAcceleration = Mathf.Clamp(movementAcceleration, 0f, 1f);
//move
transform.Translate(lastDirection * movementSpeed * movementAcceleration * Time.deltaTime, Space.World);
//audio moet nog gebeuren
//rotation tank
if (direction.magnitude > 0)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed* Time.deltaTime);
I hope this helps a bit
Answer by Cornelis-de-Jager · Aug 15, 2019 at 09:39 PM
Hi @Dlethrox, your solution is very close to what it should be. I mean really close! Here is a tip that will help you to remember direction:
var direction = [Destination] - [Origin]
Now for the solution... Change this:
transform.position = Vector3.MoveTowards(transform.position, moveDirection, step);
To this:
transform.position = Vector3.MoveTowards(transform.position, transform.position + moveDirection, step);