Search Unity

[C#] Raycast Trouble!

Discussion in 'Scripting' started by Dsavage13, Nov 27, 2015.

  1. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    im at a loss. i hope someone out there replies to THIS thread at least. im trying to make raycasting work with crouching. just a simple raycast up, but i cant get it to detect the environment. can anyone take a look please? ive tried EVERYWHERE:

    Code (CSharp):
    1. if (isCrouching == true)
    2.         {
    3.  
    4.      
    5.                         if(Physics.Raycast(transform.position, Vector3.up, 1.5f))
    6.                         {
    7.                         if(hit.collider.tag == "enviroment")
    8.                         {              
    9.                         CeilingHit = true; // stops the player from standing
    10.                         }
    11.                         }
    12.             h = 1;
    13.             movementspeed = crouchspeed;
    14.  
    15.         }


    PLEASE help me, ive been on this issue for 3 days...
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    You aren't actually getting any hit data out from the function, for one. I also added in Debug.Log calls. These are really useful since they'll print to the console the message you put into them, and can help you figure out what your code is doing.

    Code (csharp):
    1. if (isCrouching == true)
    2. {
    3.     RaycastHit hit;  
    4.     if (Physics.Raycast(transform.position, Vector3.up, out hit, 1.5f))
    5.     {
    6.         Debug.Log(hit.gameObject.name)
    7.         if(hit.collider.tag == "enviroment")
    8.         {            
    9.             Debug.Log("Hitting the ceiling!")
    10.             CeilingHit = true; // stops the player from standing
    11.         }
    12.     }
    13.    
    14.     h = 1;
    15.     movementspeed = crouchspeed;
    16. }
     
  3. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    Didnt work, its just not detecting the enviroment, i have it tagged too..
     
    Last edited: Nov 27, 2015
  4. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Debug messages are displayed here:



    What I added was the out hit parameter to the Raycast function call. hit is a variable of type RaycastHit, which I declared right above. When you pass a parameter to the function with the keyword out in front of it, it means that this object is going to be created by the function call and filled with data. In this case, Raycast takes the object and fills it with the hit data.

    I also formatted your code to make it nicer. It sounds like you're new, so I'd recommend bookmarking the Unity scripting reference page.

    Erik
     
  5. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    thank you for taking the time for me. it dosent seem to work yet., could it be the map? i made it in 3ds max and tagged it with enviroment, hm.
     
  6. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    ..any ideas?
     
  7. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Do you not have a Mesh Collider as a game component in your game object and a Mesh exported from 3ds Max that is set to that Mesh Collider?
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    tags have to be spelt the same (environment is misspelt in your script, might not be in your tags list).
    raycast only work against colliders, if you've just imported the model into unity it doesn't have any, just meshes.
     
  9. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    the part im trying to crouch under is part of the mesh that is the land i walk on, i just made the whole map in 3ds max and imported it, i can walk on it, so it must have a collider right?
     
  10. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I was referring to your character. The mesh you can walk on could have some other components allowing your character to walk on.

    NavMesh <---> NavMeshAgent
    Rigidbody <----> Mesh Collider

    If you are unsure, take a big picture of your Inspector showing your landscape terrain mesh, and your character. Show all the components (don't need all of them expanded).
     
  11. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,819
    I recommend using Debug.DrawRay
    It draws a line 100.0m in the editor so you can see what the raycast is doing

    Code (csharp):
    1.  
    2.          Debug.DrawRay(transform.position, 100.0f * Vector3.up, Color.blue, 3.0f);
    3.  
    Is the ray at the player's location?

    This is the first step in the trouble shooting process.
     
  12. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    ok, still havent figured this out. i really have tried everything. can anyone help me? i dont want to try triggers, but i will as a last resort, i dont want to have to rely on them. but i need help still. can anyone help?
     
  13. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,819
    Did you try adding the Debug.DrawRay statement?.

    Try this:
    Code (csharp):
    1.  
    2.   Debug.DrawRay(transform.position, 1.5f * Vector3.up, Color.blue, 3.0f);
    3.  
    is 1.5 a sufficient length? Is the ray starting out where it should? Can you see the ray passing through the mesh? Is the ray starting out inside a collider? How many colliders does it hit before it hits the ceiling? Is it going in the direction you want it to?

    Why don't you post a screenshot of the ray in the editor. I'm sure someone could figure out pretty quickly what the problem is.
     
    Last edited: Dec 13, 2015
  14. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    how do i see the ray if its first person? look up?
     
  15. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,819
    The Debug.DrawRay is only shown in the editor. It lasts for 3 second so u can pause the editor and see the ray.
    ray.png

    The ray is in blue. It depicts what the raycast would look like if you could see it. Me raycast used to detect if player can stand or jump.
    j