0

I'm trying to make the enemy object face the end of the line cast. The code I have locks the ray onto the player when detected properly. My problem here is that my enemy sprite is a grandchild of the Rigidbody2D and I'm using eulerAngles to change the sprites direction. Here is the code I have:

using UnityEngine;

public class PlayerDetection : MonoBehaviour
{
public Transform origin, end, player;
public float radarSpd;
public bool playerDetected;

public static bool playerIsDetected;

private int playerLayer = 1 << 8;
private Rigidbody2D enemyRb;
private Vector3 facePlayer;

private void Start()
{
    enemyRb = GetComponentInParent<Rigidbody2D>();
    playerIsDetected = false;
}

private void Update()
{
    PlayerDetector();
    if (playerDetected == false)
    {
        Radar();
    }
    else { PlayerIsDetected(); }

}

void PlayerDetector()
{
    Debug.DrawLine(origin.position, end.position, Color.red);
    playerDetected = Physics2D.Linecast(origin.position, end.position, playerLayer);
}

void Radar()
{
    end.RotateAround(origin.position, Vector3.forward, radarSpd * Time.deltaTime);
}

void PlayersPosition()
{
    facePlayer = player.position - enemyRb.transform.GetChild(0).GetChild(0).position;
    float enemyRot = Mathf.Atan2(facePlayer.y, facePlayer.x) * Mathf.Rad2Deg;
    enemyRb.transform.GetChild(0).GetChild(0).eulerAngles = new Vector3(0, 0, enemyRot);

}

void PlayerIsDetected()
{
    if(playerDetected == true)
    {
        playerIsDetected = true;
        end.position = player.position;
        PlayersPosition();
    }
}
}

The main focus for this code that I need help is here:

void PlayersPosition()
{
facePlayer = player.position - enemyRb.transform.GetChild(0).GetChild(0).position;
float enemyRot = Mathf.Atan2(facePlayer.y, facePlayer.x) * Mathf.Rad2Deg;
enemyRb.transform.GetChild(0).GetChild(0).eulerAngles = new Vector3(0, 0, enemyRot);

}

The enemy does not face the player at all and doesn't seem to rotate much at all as the player moves around the screen.

I've tried applying a rotationSpeed variable and multiplied it by enemyRot multiplied by Time.deltaTime but didn't work either.

 enemyRb.transform.GetChild(0).GetChild(0).eulerAngles = new Vector3(0, 0, enemyRot * rotSpd * Time.deltaTime);

I've been stuck on this for a week now and really need some help resolving this issue! So how can I achieve my desired rotation by use of eulerAngles?

AntonioTorro
  • 199
  • 4
  • 20
  • 1
    You could use the LookAt method here? And lerp the value. – Branden Nov 10 '19 at 07:53
  • Hello, thank you for your response! Sorry that it took me so long to respond, I had forgotten about this post. At first, I tried the LookAt() but unfortunately this is not for 2D development. The solution that I found turned out to be much more simple and didn't involve complex calculations as I thought it would. I am about to post the answer to this in a few. Thanks again for your response! – AntonioTorro Nov 16 '19 at 23:30

1 Answers1

0

Turned out that I was doing too much math. Here is the solution:

Edit: My enemy is facing in an upward position. This code works on that scale.

void PlayersPosition()
{
    facePlayer = player.position - enemyRb.transform.GetChild(0).GetChild(0).position;
    enemyRb.transform.GetChild(0).GetChild(0).up = -facePlayer;
}
AntonioTorro
  • 199
  • 4
  • 20