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 /
  • Help Room /
avatar image
0
Question by Calum_1015 · Feb 27, 2016 at 06:25 AM · 2dcollisionraycastphysics2dcollision detection

2D collision trouble, character falling through map

I am currently working on a 2d platformer. I started by making my level, which consists of several 2d sprites named "platform" (this will become evidently needed later). I then started scripting my character. I ran the code for the first time, and my character moved! Yay! Then it fell through the map. What?

Alright, here's my characters code:

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     Transform player;
 
     float playerSpeed;
     float jumpSpeed;
     float gravity;
 
     private Vector2 moveDirection;
 
     void Start ()
     {
         player = transform;
         playerSpeed = 7.5f;
         jumpSpeed = 8.5f;
         gravity = 5;
     }
     
     void Update ()
     {
         moveDirection.x = playerSpeed * Time.deltaTime;
 
         if(!isGrounded())
         {
             moveDirection.y -= gravity * Time.deltaTime;
         }
 
         if(Input.GetButtonDown("Jump"))
         {
             if (isGrounded())
             {
                 movePlayer(Vector2.up * jumpSpeed * Time.deltaTime);
             }
         }
 
         movePlayer(moveDirection);
     }
 
     void movePlayer(Vector2 direction)
     {
         player.Translate(direction);
     }
     
     bool isGrounded()
     {
         RaycastHit2D hitInfo;
 
         hitInfo = Physics2D.Raycast(player.position, -Vector2.up, 0.01f);
         if (hitInfo)
         {
             if (hitInfo.transform.gameObject.name == "platform")
             {
                 return true;
             }
             else
             {
                 return false;
             }
         }
         else
         {
             return false;
         }
     }
 }
 

As you can see, I didn't want to use the character controller (I prefer originality). So I check if the character's grounded using Raycast2D. I believe this is the source of my problem, but I'm not sure. I get no errors from my script at all. I have checked, and the platform sprite is on the same "z" axis as the character's sprite is. I could use 3D shapes, but that would be un-needed while I'd be in orthographic view for the entire game. Help would be much appreciated, and if you need screenshots just tell me, and I'll give them up just as soon as I can!

Edit - I am also using Unity 5.1.1F1

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 Calum_1015 · Feb 27, 2016 at 06:38 PM 0
Share

Really stumping me! Can't find any code in my script that would do this.

avatar image EmHuynh Calum_1015 · Feb 28, 2016 at 07:18 PM 0
Share

Hey, @Calum_1015. Check out this answer: http://answers.unity3d.com/questions/707526/check-if-2d-character-is-grounded.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by antonsem · Nov 12, 2018 at 07:22 PM

The first thing that comes to mind is that if you casting a ray from inside a collider, the ray would hit that particular collider. So most probably your ray is hitting the player all the time. Try to assign another GameObject as a ray origin and putting that object just beneath the player's collider. Like so:

  using UnityEngine;
  using System.Collections;
  
  public class Player : MonoBehaviour {
  
      Transform player;
      public Transform rayOrigin; //assign this from the inspector
      float playerSpeed;
      float jumpSpeed;
      float gravity;
  
      private Vector2 moveDirection;
  
      void Start ()
      {
          player = transform;
          playerSpeed = 7.5f;
          jumpSpeed = 8.5f;
          gravity = 5;
      }
      
      void Update ()
      {
          moveDirection.x = playerSpeed * Time.deltaTime;
  
          if(!isGrounded())
          {
              moveDirection.y -= gravity * Time.deltaTime;
          }
  
          if(Input.GetButtonDown("Jump"))
          {
              if (isGrounded())
              {
                  movePlayer(Vector2.up * jumpSpeed * Time.deltaTime);
              }
          }
  
          movePlayer(moveDirection);
      }
  
      void movePlayer(Vector2 direction)
      {
          player.Translate(direction);
      }
      
      bool isGrounded()
      {
          RaycastHit2D hitInfo;
  
          //use rayOrigin here instead of player
          hitInfo = Physics2D.Raycast(rayOrigin.position, -Vector2.up, 0.01f);
          if (hitInfo)
          {
              if (hitInfo.transform.gameObject.name == "platform")
              {
                  return true;
              }
              else
              {
                  return false;
              }
          }
          else
          {
              return false;
          }
      }
  }

And here is the hierarchy:

alt text


ss-5.png (196.3 kB)
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

91 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

Related Questions

Compatible collision constraint methods 0 Answers

2D colliders doesnt work with Gravity Scale = 0 0 Answers

Raycast on moving object? 2 Answers

2D Collission - Raycast mouseposition efficiently 0 Answers

Checking for Raycast distances not working as expected. 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