Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fudge5962 · Oct 05, 2018 at 07:50 AM · raycastcolliderjump

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fudge5962 · Oct 05, 2018 at 05:20 PM 0
Share

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?

avatar image fudge5962 fudge5962 · Oct 05, 2018 at 05:24 PM 0
Share

Answer: yes. A layer mask will prevent my Raycast from detecting things it shouldn't.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by LCStark · Oct 05, 2018 at 08:12 AM

  1. What does your "ground" object look like?

  2. Use Debug.DrawRay to see if your raycast works correctly.

  3. "Why is this not working?" is not really a good question. Be more specific when creating new posts.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fudge5962 · Oct 05, 2018 at 05:14 PM 0
Share

@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"?

avatar image LCStark fudge5962 · Oct 05, 2018 at 06:05 PM 1
Share

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.
avatar image fudge5962 LCStark · Oct 05, 2018 at 08:04 PM 0
Share

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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

RaycastHit collider not showing correct tags 1 Answer

Problem with Mesh Collider and Raycasting 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges