1
0
Fork 0
Bildschirmflausch-LD41/Assets/Scripts/EnemyAI.cs

47 lines
1.3 KiB
C#
Raw Normal View History

2018-04-22 02:07:00 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour {
2018-04-22 17:00:12 +02:00
private GameObject victim;
Rigidbody2D body;
[SerializeField]
private float speed = 1;
[SerializeField]
private float rotationSpeed = 1;
/*
2018-04-22 02:07:00 +02:00
* Die destructive dumme deutsche Dungeon Diktator Drifter DLC Debakel Distribution Dokumentations - Druck Datei
2018-04-22 17:00:12 +02:00
*/
// Use this for initialization
void Start () {
victim = null;
body = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
if(victim == null) {
victim = GameController.instance.GetPlayer().gameObject; // TODO testing purpose only!
2018-04-22 11:27:09 +02:00
return;
}
2018-04-22 17:00:12 +02:00
Vector3 distanceToEnemy = victim.transform.position - gameObject.transform.position;
// movement
2018-04-22 17:00:12 +02:00
body.velocity = new Vector2(distanceToEnemy.normalized.x, distanceToEnemy.normalized.y) * speed;
2018-04-22 02:07:00 +02:00
2018-04-22 17:00:12 +02:00
//rotation
Vector3 localRotation = gameObject.transform.localRotation * Vector3.up;
float angleToRotate = Mathf.Round(Vector3.SignedAngle(localRotation, distanceToEnemy.normalized, Vector3.forward));
gameObject.transform.Rotate(0, 0, angleToRotate * rotationSpeed);
}
2018-04-22 02:07:00 +02:00
public void SetVictim(GameObject g) {
2018-04-22 11:27:09 +02:00
victim = g;
}
2018-04-22 02:07:00 +02:00
}