Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
941 B
1
Indexable
Never
public void LockOnEnemy()
	{
		LayerMask lockOnLayer = LayerMask.GetMask("Enemy");
		
		Collider[] enemies = Physics.OverlapSphere(transform.position, lockOnRadius, lockOnLayer);
		
		if (enemies.Length > 0)
		{
			Debug.Log("Targeting " + enemies[0].name);
			float nearestDistance = Mathf.Infinity;
			foreach (Collider enemy in enemies)
			{
				float distance = Vector3.Distance(transform.position, enemy.transform.position);
				if (distance < nearestDistance)
				{
					nearestDistance = distance;
					targetEnemy = enemy.transform;
				}
			}
		}
		else 
		{
			targetEnemy = null;
		}
		
		if (targetEnemy != null)
		{
			// Rotate the player towards the target enemy.
			Vector3 direction = (targetEnemy.position - aimLockPoint.position).normalized;
			direction.y = 0;
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 10f);
		}
	}