- Home /
Raycast always colliding (SOLVED)
I'm trying to draw a simple RayCast to see if I'm on the ground or not:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeTest : MonoBehaviour {
public float mouseSensitivity = 15.0f;
public float moveSpeed = 15.0f;
//this handles the camera rotation
private float camRot;
private float distToGround;
// Use this for initialization
void Start () {
//initialize the camRot to the starting rotation
camRot = 30.0f;
//initialize the camera child rotation
transform.Find("Main Camera").localRotation = Quaternion.Euler(30.0f, 0, 0);
//Gets the distance to the bottom of the capsule collider
distToGround = GetComponent<Collider>().bounds.extents.y;
}
// Update is called once per frame
void Update () {
MoveMe();
}
void MoveMe()
{
//get the X axis rotation used for the game object rotation
var xRot = Input.GetAxis("Mouse X") * Time.deltaTime * 20.0f * mouseSensitivity;
//get the rotation used for the camera child rotation
camRot += Input.GetAxis("Mouse Y") * Time.deltaTime * 10.0f * mouseSensitivity;
//get the X and Z axis movement used for the game object movement
var xTran = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
var zTran = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
//clamp the rotation for the camera
camRot = Mathf.Clamp(camRot, -30f, -7.5f);
//set the rotation for the camera
transform.Find("Main Camera").localRotation = Quaternion.Euler(-camRot, 0, 0);
//rotate the game object
transform.Rotate(0, xRot, 0);
//move the game object
transform.Translate(xTran, 0, zTran);
if (Input.GetKeyDown("space") && IsGrounded())
{
gameObject.GetComponent<Rigidbody>().AddForce(transform.up * 10f, ForceMode.Impulse);
}
}
bool IsGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.3f);
}
}
No matter what I do, I can still jump in midair infinitely. Why is the check not working?
After further investigation, I've discovered that the problem is my raycast is detecting the game object itself and occasionally returning true for that. I added the game object to the raycast ignore layer and it stopped happening. I can't leave the game object in there, however. I need it to be detectable by other raycasts. Can I solve this problem with a layer mask on my Physics.Raycast call?
Answer: yes. A layer mask will prevent my Raycast from detecting things it shouldn't.
Answer by LCStark · Oct 05, 2018 at 08:12 AM
What does your "ground" object look like?
Use
Debug.DrawRay
to see if your raycast works correctly.
"Why is this not working?" is not really a good question. Be more specific when creating new posts.
@LCStark $$anonymous$$y ground is a terrain. I added a Debug.DrawRay and saw that the size of my Raycast was indeed off, but that was not helpful to solving my problem. It only prevented a further problem.
I'm not sure what better question I could really ask. The code is extremely simple. It should draw a line straight down to just a little farther than the bottom of my object's collider, which it does, and check to see if there is anything there, which it does. It should return true if there is, and false if there isn't, which it sometimes does, but sometimes it will randomly return true for no reason. What question can I ask that's more specific? "Why is it returning true when there's nothing there"?
Debug.DrawRay
draws a line in the provided direction, but you have to provide the length of the line yourself (by multiplying the direction). If you give it -Vector3.up
as a direction, it will use that vector's length (which is 1.0). $$anonymous$$ultiply it by your (distToGround + 0.3) to get the proper length.
As for the question - it's not about the description of your post, it's about the title. When people browse the question list and see "Why is it not working?" it doesn't tell them anything about what you are looking for. If you named it "Why is my raycast always colliding?" or "Raycast always returns true" people can instantly know what your question is about. Imagine what that question list would look like if all questions were named like this:
help me
i have a problem
this doesn't work
i have an error
Now, back to your problem: I haven't worked with Terrain much, but the test setup I made works as intended. I suggest you try three things:
- check the value of your distToGround
- this would be the most probable cause, if the GetComponent<Collider>().bounds.extents.y;
returned value greater than expected
- use the raycast version returning more information to RaycastHit
:
public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance);
You can use the
hitInfo.distance
to check the distance between the origin of the ray to the hit point.
- using the
RaycastHit
check the name / tag of the object the ray is hitting. You're not using any layer mask, so perhaps your raycast is hitting something else.
I did update my question to be more informative, and to note that I did find a solution. It was the lack of layer mask, causing my raycast to go through the game object itself, and always return true.
Thank you for helping me debug the issue. I have found a second issue, though. When I walk towards a vertical surface, like a wall, I will gain upward momentum for as long as I try to walk towards it, eventually flying high into the sky for no apparent reason. Do you know what causes this and how I can stop it?
Your answer
Follow this Question
Related Questions
Objects stops if it lands in between two colliders 1 Answer
My Player keeps launching into the air 4 Answers
IsNormalized Error? 0 Answers