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): if (isCrouching == true) { if(Physics.Raycast(transform.position, Vector3.up, 1.5f)) { if(hit.collider.tag == "enviroment") { CeilingHit = true; // stops the player from standing } } h = 1; movementspeed = crouchspeed; } PLEASE help me, ive been on this issue for 3 days...
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): if (isCrouching == true) { RaycastHit hit; if (Physics.Raycast(transform.position, Vector3.up, out hit, 1.5f)) { Debug.Log(hit.gameObject.name) if(hit.collider.tag == "enviroment") { Debug.Log("Hitting the ceiling!") CeilingHit = true; // stops the player from standing } } h = 1; movementspeed = crouchspeed; }
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
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.
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?
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.
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?
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).
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): Debug.DrawRay(transform.position, 100.0f * Vector3.up, Color.blue, 3.0f); Is the ray at the player's location? This is the first step in the trouble shooting process.
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?
Did you try adding the Debug.DrawRay statement?. Try this: Code (csharp): Debug.DrawRay(transform.position, 1.5f * Vector3.up, Color.blue, 3.0f); 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.
The Debug.DrawRay is only shown in the editor. It lasts for 3 second so u can pause the editor and see the ray. 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