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 ToeBeanGames · Jul 18, 2014 at 07:37 PM · collision detectionray cast

Ray casting to detect ground/ceiling collision

I'm working on a side scroller controller.

What I have so far is enums that determine what direction you're aiming based on what arrow keys are pressed (Forward and up, as well as down while in air) and one for the player state.

I have it set up so when the jump key is pressed and the player state is neither jumping or falling, you jump. The problem is, the behavior can't detect collision with the level mesh yet, so you can only jump once and then you're stuck in Falling forever instead of switching to Idle.

Can someone please help me understand how to use ray casting to detect ground collision?

It would also work for ceiling too because I need it to switch from jumping to falling when the ceiling is hit.

Here's the code I have so far:

 using UnityEngine;
 using System.Collections;
 public class PlayerController : MonoBehaviour {
 
     #region Animations
     public AnimationClip Idle;
     public AnimationClip IdleLookingUp;
     public AnimationClip Walk;
     public AnimationClip WalkLookingUp;
     public AnimationClip Jump;
     public AnimationClip JumpLookingUp;
     public AnimationClip JumpLookingDown;
     public AnimationClip Fall;
     public AnimationClip FallLookingUp;
     public AnimationClip FallLookingDown;
     #endregion
 
     #region Variables
     public float WalkSpeed;
     public float Gravity;
     public float JumpForce;
     bool IsGrounded;
     bool HasJumped;
     bool HasHitCeiling;
     Vector3 Forward;
     CharacterController controller;
     Object LevelMesh;
     Object Player;
     #endregion
 
     #region Enums
     enum PlayerDirection
     {
         Forward = 0,
         Up = 1,
         Down = 2
     }
     enum PlayerState
     {
         Idle = 0,
         Walking = 1,
         Jumping = 2,
         Falling = 3,
         Dead = 4
     }
     PlayerDirection playerDirection;
     PlayerState playerState;
     #endregion
 
     // Use this for initialization
     void Start () {
         Forward = transform.forward;
         LevelMesh = GameObject.FindGameObjectWithTag ("Level");
         Player = gameObject;
     }
     
     // Update is called once per frame
     void Update () {
         print ("State: " + playerState + "; Direction: " + playerDirection);
         //MOVE THE PLAYER IN FORWARD AT WALKSPEED
         //JUMP
         if (Input.GetButtonDown ("Jump") && playerState != PlayerState.Jumping && playerState != PlayerState.Falling)
         {
             rigidbody.AddForce (new Vector3(0, JumpForce, 0));
             //print ("You are jumping.");
             playerState = PlayerState.Jumping;
             IsGrounded = false;
         }
         if (Input.GetButtonUp ("Jump") && playerState == PlayerState.Jumping)
         {
             playerState = PlayerState.Falling;
             IsGrounded = false;
         }
         //FALL WHEN CEILING IS HIT
         //ALSO BE ABLE TO LOOK UP WHILE GROUNDED
         //AND LOOK UP & DOWN WHILE IN AIR
     }
 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WilliamLeu · Jul 23, 2014 at 03:03 AM

Easiest thing I can think of is to use Physics.SphereCast or Physics.SphereCastAll at the bottom of the collider where you expect the bottom of the character to touch to ground to see if your character is grounded. Just make sure to filter out the collision with your character's collider.

And for falling, check if the Y component of your character's rigidbody velocity is negative.

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

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

22 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

Related Questions

I want to make a character jump and crouch without using boolean 0 Answers

OnTriggerStay working in Editor, but not on Android device 0 Answers

CapsuleCast is detecting the wrong hit normal when colliding with two surfaces 0 Answers

Why is the material switching back after collision? 1 Answer

Checking to see where Roulette ball landed in the wheel 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