using UnityEngine;
using System.Collections;
public class AIEnemy : MonoBehaviour {
public GameObject bullet;
public float bulletSpeed = 10;
public Transform player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
}
void OnTriggerStay2D(Collider2D col)
{
if(col.tag == "Player")
{
// Debug.DrawRay(transform.position, player.position - transform.position);
RaycastHit2D rhit2d = Physics2D.Raycast(transform.position, player.position - transform.position, Mathf.Infinity);
if (rhit2d.collider.tag == "Player")
{
var clone = Instantiate(bullet, transform.position, Quaternion.identity) as Rigidbody2D;
clone.velocity = (player.position - transform.position * bulletSpeed);
}
}
}
}
So this is my code. When the player stays in the trigger collider it will find the player and fire at it. But my problem is that the bullet doesn't go towards the player it just falls down. I have set the bullet as a trigger so that isn't the problem. (It's a 2D game btw) The project is on Github: https://github.com/Massaxe/2D-AI/