Hi, so I’m trying to make a combat lock on system, where the player uses the right joystick, points in the rough direction of an enemy, and then the player can lock on to them.
The video below demonstrates the current state, along with the problem I face:
The black gizmo shows the right stick input, while the blue gizmo line shows the target that it’s locked on to currently. (Red gizmo is irrelevant as it shows current player input).
The problem is, when the player is close, the SphereCast does not detect the enemy, as demonstrated in the video.
This is my code:
lockInput = new Vector3(aimInput.x, 0, aimInput.y);
var matrix = Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0));
skewedLock = matrix.MultiplyPoint3x4(lockInput);
Vector3 lockHitPoint = skewedLock.normalized;
RaycastHit info;
if (Physics.SphereCast(lockDetectionOffset.position, lockDetectionRadius, lockHitPoint, out info, maximumLockRange, enemyLayer))
{
if (info.collider.CompareTag("Enemy"))
{
lockOnTarget = info.collider.gameObject.transform;
}
}
Looking into it further, I see this on the unity documentations, which seems to somewhat explain my error
SphereCast will not detect colliders for which the sphere overlaps the collider.
What can I do to remedy this? Are there any alternatives? Most videos I’ve seen regarding such systems say to use SphereCast(mainly Mix&Jam’s one).