I’m sorry for the vague title! I think my question is best explained with an image:
Allow me to explain:
The image on the left shows my character raycasting to a wall object, to which the character needs to move next to, in order to hide behind the cover that the wall provides.
The problem occurs when the raycast (red line) hits the very end of the wall.
The user moves to cover, but as shown in the image on the right, slightly hangs over the edge of the cover thus losing the illusion that the character is safely behind a wall.
The character needs to be at least within the green box specified, even if the raycast hits the edge of the wall object.
Taking the above points into account, is there any checks or methods you can provide me to solve this niggling issue?
I’m assuming that what you’re doing is having the raycast emanate from the character’s transform.position to the wall’s transform.position and making the character approach the wall until both of their transform.position are aligned. If that’s the case, and assuming that I actually understand what you’re trying to do, then just have the min/max of the bounds of either the renderer or collider of your character be the origin of the raycast.
You’ve just given me an idea, actually. Instead of using Raycasts for everything, which seemed a bit “hacky”, could I use “ClosestPointOnBounds” to get the closest cover point and then manually alter the vector3 to set the player X position within the bounds of the object? Would get rid of raycasts completely, right?
ClosestPointOnBounds() returns the point in all of your game object’s colliders which is closest to a certain position. You could prolly use it the way you described, assuming that the correct object is using the function and the correct position is being passed to it.