Hallo!
I’ve got a script witch should let the enemies go to player then he is in range. the problem is, the enemies are not moving.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour {
public Transform player;
Vector2 destination;
Vector2 place;
Vector2 journey;
// Max and mit Dastance to Move
float minDistanceToPlayer = 0.5f;
float maxDistanceToPlayer = 4f;
public int speed = 50;
Rigidbody2D rb;
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
void Update () {
moveToPlayer ();
lookAtPlayer ();
}
void moveToPlayer() {
destination = player.transform.position;
place = transform.position;
journey.x = destination.x - place.x;
journey.y = destination.y - place.y;
if(Vector2.Distance(place, destination)<4 ) {
rb.velocity = new Vector2 (journey.normalized.x * speed * Time.deltaTime, journey.normalized.y * speed * Time.deltaTime);
Vector2 v = rb.velocity;
float angle = Mathf.Atan2 (v.y, v.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
}
void lookAtPlayer() {
// float rotation_z = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;
// transform.rotation = Quaternion.Euler (0, 0, rotation_z );
}
}
Thank you for help!