How do i make an ai shoot at player? 2D
Hi im making a top down 2d game and i need the enemy to shoot at the player. I have a bullet made and the enemy follows the player. I do not want the enemy to rotate at all i just want it to shoot at the player.
Heres my code
using UnityEngine;
using System.Collections;
public class AiZ : MonoBehaviour
{
public Transform target;
public float speed = 3f;
void Start()
{
}
void Update()
{
var step = speed * Time.deltaTime;
if (Vector3.Distance(transform.position, target.position) < 10f) {
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
if (Vector3.Distance(transform.position, target.position) < 9f)
{
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Shooting animation and bullet generation issue 0 Answers
Shooting bullets in mouse direction for 2D game 1 Answer
Player Respawn Problem, Bullets dont move! [C#] 0 Answers
How can I make my player's gun point at the cursor, not the player itself? 1 Answer
Instantiating object at wrong location, Unity bug? 0 Answers