Hello,
I have this method:
public static Quaternion FaceObject(Vector2 myPos, Vector2 targetPos)
{
Vector2 direction = targetPos - myPos;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
return Quaternion.AngleAxis(angle, Vector3.forward);
}
This method gets called every second with the nearest enemy position as targetPos, and the position of the object for myPos.
Now the problem is, I first used transform.LookAt (didn’t work), then I searched for LookAt in 2D and tried different solutions, no of them worked. Does anyone know the solution?
Whole script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class BotScript : MonoBehaviour {
public float health = 100f;
public float framesUntilNextShoot = 90f;
public GameObject shootPrefab = null;
public static Quaternion FaceObject(Vector2 myPos, Vector2 targetPos)
{
Vector2 direction = targetPos - myPos;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
return Quaternion.AngleAxis(angle, Vector3.forward);
}
// Initalize player
void Start()
{
float x = Random.Range(-4.1f, 4.1f);
float y = Random.Range(-4, 3.8f);
health = 100f;
gameObject.transform.position = new Vector2(x, y);
}
void FixedUpdate()
{
framesUntilNextShoot -= 1;
//if (DistanceToClosestEnemy() < 300f)
//{
GameObject go = FindClosestEnemy();
this.transform.rotation = FaceObject((Vector2)transform.position, (Vector2)go.transform.position);
if(framesUntilNextShoot == 0f)
{
framesUntilNextShoot = 90f;
GameObject inst = Instantiate(shootPrefab, gameObject.transform.position, gameObject.transform.rotation);
inst.transform.parent = this.transform;
//print("Shoot pos: " + inst.transform.position.x + " " + inst.transform.position.y + " Script pos: " + this.transform.position.x + " " + this.transform.position.y + " GameObject pos: " + this.gameObject.transform.position.x + " " + this.gameObject.transform.position.y);
inst.GetComponent<ProjectileScript>().StartShoot(go);
}
Vector2 movement = (1f * (this.transform.rotation * Vector2.up))*Time.deltaTime;
this.transform.position = new Vector2(this.transform.position.x, this.transform.position.y) + movement;
//}
}
// Update is called once per frame
void Update () {
if(health < 1)
{
Destroy(this.gameObject);
}
}
private void LookAt(Transform go)
{
Vector2 direction = new Vector2(go.position.x - this.transform.position.x, go.transform.position.y - this.transform.position.y);
float rotation = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
this.transform.eulerAngles = new Vector3(0, 0, rotation);
}
public GameObject FindClosestEnemy()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Enemy").Concat(GameObject.FindGameObjectsWithTag("Player")).ToArray();
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
public float DistanceToClosestEnemy()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Enemy");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return distance;
}
}