1
0
Fork 0

enemys now rotate, close #16

This commit is contained in:
Jan 2018-04-22 17:00:12 +02:00
parent 80232d1c27
commit eb1e15c52f
2 changed files with 32 additions and 29 deletions

View file

@ -113,8 +113,8 @@ Rigidbody2D:
m_UseFullKinematicContacts: 0
m_UseAutoMass: 0
m_Mass: 1
m_LinearDrag: 0
m_AngularDrag: 0
m_LinearDrag: 0.1
m_AngularDrag: 0.01
m_GravityScale: 0
m_Material: {fileID: 0}
m_Interpolate: 1
@ -157,10 +157,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 582bfd99a32c0ea45a7d09ef4f308d88, type: 3}
m_Name:
m_EditorClassIdentifier:
victim: {fileID: 0}
agressor: {fileID: 1971451785855062}
speed: 0.2
rotaionSpeedWIP: 1
speed: 0.7
rotationSpeed: 1
--- !u!212 &212571453617041228
SpriteRenderer:
m_ObjectHideFlags: 1

View file

@ -4,35 +4,40 @@ using UnityEngine;
public class EnemyAI : MonoBehaviour {
[SerializeField]
private GameObject victim;
[SerializeField]
private GameObject agressor;
Rigidbody2D body;
[SerializeField]
private float speed = 1;
[SerializeField]
private float rotaionSpeedWIP = 1;
private float rotationSpeed = 1;
/*
* Die destructive dumme deutsche Dungeon Diktator Drifter DLC Debakel Distribution Dokumentations - Druck Datei
* */
*/
// Use this for initialization
void Start () {
victim = null;
body = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update() {
void Update ()
{
if(victim == null) {
//victim = GameController.instance.GetPlayer().gameObject; // TODO testing purpose only!
victim = GameController.instance.GetPlayer().gameObject; // TODO testing purpose only!
return;
}
Vector3 distanceToEnemy = victim.transform.position - gameObject.transform.position;
// movement
agressor.transform.Translate(Vector3.Scale(( victim.transform.position - agressor.transform.position ).normalized, new Vector2(speed, speed)));
body.velocity = new Vector2(distanceToEnemy.normalized.x, distanceToEnemy.normalized.y) * speed;
//rotation
agressor.transform.Rotate(agressor.transform.forward, Vector3.Angle(victim.transform.position - agressor.transform.position, agressor.transform.rotation * new Vector3(1, 1, 1)));
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);
}
public void SetVictim(GameObject g) {