So the problem is, I’m working with 2D sprites in a 3D environment and when the sprite is directly vertical to the camera, you can’t see it. Now, I managed to get the player working by tracking mousePos
;
if (lookAtMouseEnabled)
{
Lookatmouse();
}
Vector3 mousePos = Input.mousePosition;
//Debug.Log($"Mouse X: {mousePos.x}");
if (mousePos.x >= minXRotation && mousePos.x <= maxXRotation)
{
lookAtMouseEnabled = false;
}
else
{
lookAtMouseEnabled = true;
}
However, I don’t believe I can do the same with the Enemies? I was thinking of tracking if whether or not the player is on the left or right and stopping the rotation 20f
before the enemy is looking directly at the player and vice versa;
public Transform player;
public float speed = 5.0f;
public float rotationSpeed = 10.0f;
public float chaseDistance = 2.0f;
public float stoppingDistance = 1.0f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (player == null)
{
GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
if (playerObject != null)
{
player = playerObject.transform;
}
}
if (player != null)
{
Vector3 direction = player.position - transform.position;
float distance = direction.magnitude;
if (distance > stoppingDistance)
{
//Rotation
direction.Normalize();
Quaternion lookRotation = Quaternion.LookRotation(direction);
rb.rotation = Quaternion.Slerp(rb.rotation, lookRotation, rotationSpeed * Time.fixedDeltaTime);
//ChasePlayer
Vector3 chasePosition = player.position - player.forward * chaseDistance;
Vector3 newPosition = Vector3.MoveTowards(rb.position, chasePosition, speed * Time.fixedDeltaTime);
rb.MovePosition(newPosition);
// Determine if the player is on the left or right
Vector3 enemyToPlayer = player.position - transform.position;
Vector3 enemyRight = transform.right;
float dotProduct = Vector3.Dot(enemyToPlayer, enemyRight);
if (dotProduct < 0)
{
Debug.Log("Player is on the left.");
}
else
{
Debug.Log("Player is on the right.");
}
}
}
}
I’m just wondering how to work through this 2D sprite 3D world complication I have made for myself. Any suggestions would be great.