Fixed the enemy movement and shooting track. Fix was in three steps -
Added a variable to determine the "UpperChest" of the player character. Did this because the default Skeleton placeholder is at the feet of the character.
However, the upper chest is located at the upper chest (🤓)
This change was made using the following function -
Calling it as follows in the Start function-
playerTarget = GetSkeletonPlayerTargetTransform(player);
Calculate the Difference between the player and the turret along with the relative angle of their vector positions-
Vector3 playerDirection = playerTarget.transform.position - transform.position;distance = Vector3.Distance(playerTarget.transform.position, transform.position);
Use the distance to trigger the enemy turret movement -
if(fireRateDelta <= 0 && distance < turretShootRange)
{
//* Rotate if the player is in range for the turret
Quaternion rotation = Quaternion.LookRotation(playerDirection);
Quaternion current = transform.localRotation;
transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime * turretRotationOnDetectionSpeed);`
//* Angle function checks the angle between the players position vector and enemy's position vector
if (180-Vector3.Angle(currentGun.transform.forward,playerTarget.transform.position)<=angleThreshold){
//* If the enemy is in a certain angle threshold, only then shoot
currentGun.Fire();
fireRateDelta = fireRate;
}
}
Here we use the relative angle to see if the gun of the turret can actually see the player. If it can with a threshold, only then will it shoot, or else it will shoot wherever it is looking when the player is in range.
Fixed the enemy movement and shooting track. Fix was in three steps -
Added a variable to determine the "UpperChest" of the player character. Did this because the default Skeleton placeholder is at the feet of the character. However, the upper chest is located at the upper chest (🤓) This change was made using the following function -
Calling it as follows in the Start function-
playerTarget = GetSkeletonPlayerTargetTransform(player);
Calculate the Difference between the player and the turret along with the relative angle of their vector positions-
Vector3 playerDirection = playerTarget.transform.position - transform.position;
distance = Vector3.Distance(playerTarget.transform.position, transform.position);
Use the distance to trigger the enemy turret movement -
Here we use the relative angle to see if the gun of the turret can actually see the player. If it can with a threshold, only then will it shoot, or else it will shoot wherever it is looking when the player is in range.