Hello everyone,
I’m working on a 2.5D (2D sprites in a 3D world) first person game that is all about melee combat. The enemies are also 2.5D using a box collider and sprite renderer components (among others). They always face in front of the player by rotating on the Y-axis based on the main Camera’s Euler Angle on the Y Axis:
public class Billboard : MonoBehaviour
{
private void Update()
{
Vector3 newRotation = Camera.main.transform.rotation.eulerAngles; // There's only one camera so I'm not worried about fixing this just yet
transform.rotation = Quaternion.Euler(0, newRotation.y, 0);
}
}
The issue I’m having though is that the combat works by shooting a raycast from the GameObject doing the attacking. For the player this seems to work just fine, but for the enemy it does not. This is how I’m handling attacking using a raycast:
if (Physics.Raycast(transform.position, transform.TransformDirection(transform.forward), out RaycastHit info, raycastDistance))
{
Debug.DrawLine(transform.position, transform.TransformDirection(transform.forward) * raycastDistance);
if (info.collider.gameObject.TryGetComponent<PlayerHealth>(out var playerHealth) && _canAttack)
{
Debug.Log("Got player");
animator.SetTrigger("Attacking");
playerHealth.UpdateHealth(-enemyData.attackStrength);
_canAttack = false;
StartCoroutine(CooldownAttack());
Invoke("ResetAnimation", attackDowntime);
}
}
It shoots out a raycast from the center of the enemy and if it intersects with the player (based on the player’s PlayerHealth component) and if it can attack, then it performs the animation and decreases the player’s health.
When I tested this, the enemy’s forward vector doesn’t seem to be directly in front of the enemy like I expected. Instead, it points to the origin of the game world like so:
Even without calling transform.TransformDirection()
this still happens.
Any help is appreciated. Thank you.