I have been trying to make an ai that goes to the player and attacks him, and I have looked up tutorials on how to do it but they all use a patrol system, which I don’t want to use.heres what I have so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Deformed : MonoBehaviour {
Rigidbody2D rb2D;
Rigidbody2D playerRb2D;
Transform trans;
Transform playerTrans;
public float Distancebetween;
public GameObject player;
void Start () {
rb2D = gameObject.GetComponent<Rigidbody2D> ();
trans = gameObject.GetComponent<Transform> ();
playerTrans = player.GetComponent<Transform> ();
playerRb2D = player.GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
Distancebetween = playerTrans.position.x - trans.position.x;
if (Distancebetween != 0){
rb2D.AddForce( Vector2.right * playerRb2D.velocity.x);
}
}
}