I’ve added crouching to my player, so when a button is pressed the player’s length is reduced by 50%. However, this makes him undetectable by enemies. The Raycast they use to find the player, that points right at him at all times, won’t hit him when in a smaller state. I also tried to make the size change more subtle, like 1%. Still nothing.
Are Raycasts unable to find objects when they aren’t in their starting size? How do I get around this problem?
going to need to see how you’re doing this…
There isn’t much to show. I change the player’s size with this:
if (Input.GetKeyDown(KeyCode.C))
{
if (isCrouching == false)
{
transform.localScale -= new Vector3(0, 0.5F, 0);
transform.position -= new Vector3(0, 0.5F, 0);
isCrouching = true;
}
And the enemy detects the player with this:
if(Physics.Raycast(transform.position + Vector3(0, 1, 0), fwd, hitInfo))
{
if (hitInfo.collider.gameObject.tag == ("Player"))
{
canSee = true;
}
this is based on a few assumptions about how your scene is setup, but it looks a lot like the raycast is “shooting over the head” of the crouching unit. You can add in some Debug.DrawRay(…) to check that.
I have tried that. It’s still pointing at the center of the player when he’s crouching. I also tried adding in a little something to tell what object the Raycast does hit. Most of the time it hits the floor, and the only time when it does hit the player in a crouching state was when the player is high up in the air. Could the DrawRay be lying?
I think I’ve found the problem: The player gets too small. Apparently it’s the minimum size for a Raycast to notice it. Oh well… Guess I’ll have to scale up every single game object…