Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 darkhog · Apr 03, 2013 at 01:50 AM · c#raycastraycasting

Problem with raycasting, checking for ground doesn't work

I have troubles with checking for ground in my 2.5D platformer. Here's script I use for player's movement

 using UnityEngine;
 using System.Collections;
 
 public class playerMovement : MonoBehaviour {
     public float movementForce = 0.1f;
     public float jumpForce = 1f;
     public bool ground = false;
     // Update is called once per frame
     void Update () {
         //defining ray
         Ray bottom = new Ray(this.transform.position,Vector3.down);
         //defining RaycastHit
         RaycastHit hit = new RaycastHit();
         //testing for collision, should work, but it doesn't, even if ray is halfway through object
         //this returns false no matter what I try
         bool hitground = this.collider.Raycast(bottom,out hit,20);
         if (hitground){
             //if it collided, test if it is in right distance
             ground = hit.distance<=0.6;}
         //drawing ray
         Debug.DrawRay(bottom.origin,bottom.direction);
         //logging distance and if it hit (always gives Hit distance: 0 Did hit: false)
         Debug.Log("Hit distance: "+hit.distance.ToString()+" Did hit: "+hitground.ToString());
         //movement, this works fine
         if (Input.GetAxisRaw("Horizontal")==-1){this.rigidbody.AddForce(-movementForce,0,0);}
         if (Input.GetAxisRaw("Horizontal")==1){this.rigidbody.AddForce(movementForce,0,0);}
         //jumping, this doesn't due to stupid raycasting bug.
         if ((ground)&&(Input.GetButtonDown("Jump"))){this.rigidbody.AddForce(0,jumpForce,0);}
     }
 }
 

Another thing is that I'm using rigid body for movement and spherical collision shape (player character is and will be cube as this is minimalistic game), so player can climb on smaller platforms. Theoretically it should work as ray is shown passing directly through platform (another cuboid, I'm talking about ray drawn by Debug.DrawRay), but it doesn't.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Apr 03, 2013 at 03:54 AM

Collider.Raycast() only checks for a collision between this specified collider, so you are only raycasting against the collider attached to the player game object. You want to use Physics.Raycast() instead.

Side note: you don't need to create a new RaycastHit() as you do on line 13...just declare the variable.

Comment
Add comment · Show 1 · 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 darkhog · Apr 03, 2013 at 02:10 PM 0
Share

Thanks! Your answer because of being more technical and thoroughful was promoted to best answer. Works great (though will need to cast extra rays, because when player character is little off platform, but still stand on it it returns false. Nothing I can manage though).

avatar image
1

Answer by Benproductions1 · Apr 03, 2013 at 03:55 AM

Hi! there,

If you read the documentation, you will realise, that calling this.collider.Raycast does a ray-mesh intersection test, only with that collision mesh. What you want to do is call Physics.Raycast instead, which does a ray-mesh intersection test with every collider in the scene.

Hope this helps, Benproductions1

Comment
Add comment · 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
0

Answer by josetgg · Apr 11, 2013 at 07:35 PM

I´m having the same problem but I´m actually using Physics.Raycast(), my character is standing on the ground but my function doesn´t detect the ground. If I zoom in the collision between my character collider (a box collider) and my platform collider (also a box collider) they look like if they where intersecting a little. I believe this could be the problem but I´m not sure, because if I place anothe platform right below the other one and make the raycast a bit longer then it works. If this is the problem how can I make my colliders not to intersect? if is not the problem then what could it be.

Comment
Add comment · Show 1 · 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 darkhog · Apr 11, 2013 at 08:49 PM 0
Share
  1. Put it as new question

  2. You need to cast multiple rays, from corners of bottom side and middle of edges of bottom side too and check those also.

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

12 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

Related Questions

Raycast causes all enemies to attack 1 Answer

C# Raycast Code not working 2 Answers

How to check if an empty GameObject is inside an object? 2 Answers

Cast multiple rays from an object between two angles? 0 Answers

Set raycast on player camera or object? 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