Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast Not Finding Object When Its Size Changes

Discussion in 'Scripting' started by Wilelle, Jun 25, 2015.

  1. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    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?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    going to need to see how you're doing this...
     
  3. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    There isn't much to show. I change the player's size with this:
    Code (JavaScript):
    1. if (Input.GetKeyDown(KeyCode.C))
    2.     {
    3.         if (isCrouching == false)
    4.         {
    5.         transform.localScale -= new Vector3(0, 0.5F, 0);
    6.         transform.position -= new Vector3(0, 0.5F, 0);
    7.         isCrouching = true;
    8.         }
    And the enemy detects the player with this:
    Code (JavaScript):
    1. if(Physics.Raycast(transform.position + Vector3(0, 1, 0), fwd, hitInfo))
    2.     {
    3.         if (hitInfo.collider.gameObject.tag == ("Player"))
    4.         {
    5.             canSee = true;
    6.         }
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  5. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    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?
     
    Last edited: Jun 25, 2015
  6. Wilelle

    Wilelle

    Joined:
    Jun 16, 2015
    Posts:
    93
    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...