• 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 Vane7625 · Sep 05, 2018 at 07:14 PM · 2d game2d-physicsfalling-through-floor

My 2D character is falling through the ground

Okay, so before you tell me this has been asked a thousand times, my question has a plot twist.

Here's the summary. I'm trying the 2D Platformer tutorial: Link

I've done everything the way the guy says it should be except for the script. Which I altered a tiny little bit. The issue here is that my script makes the player fall through the platform but his works just as it looks on the video.

What I'd like to know is what the issue with my script is.

Before you ask, I've done everything else the way he said: 1) I've added a Box Collider 2D to the platform. 2) The sprite already comes with Rigidbody 2D, Box collider 2D, Circle Collider 2D components and with an empty groundCheck child that is used in the c# script. The Circle Collider 2D has its own physics material called player. 3) The platform has the right tag and layer (Ground).

This is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
 
     [HideInInspector] public bool facingRight = true;
     [HideInInspector] public bool jump = true;
     public float moveForce;
     public float maxSpeed;
     public float jumpForce;
     public Transform GroundCheck;
 
     private bool grounded = false;
     private Animator anim;
     private Rigidbody2D rb2d;
     private float h;
      
     
     void Awake () {
         anim = GetComponent<Animator>();
         rb2d = GetComponent<Rigidbody2D>();
     }
 
    
     // Update is called once per frame
     void FixedUpdate () {
         
         h = Input.GetAxis("Horizontal");
         anim.SetFloat("Speed", Mathf.Abs(h));
 
         if (h * rb2d.velocity.x < maxSpeed)
         {
             rb2d.AddForce(Vector2.right * h * moveForce);
         }
 
         if (Mathf.Abs (rb2d.velocity.x)> maxSpeed)
         {
             rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
         }
 
         if (h > 0 && !facingRight)
         {
             Flip();
         }
         else if (h<0 && facingRight)
         {
             Flip();
         }
 
         if (jump)
         {
             rb2d.AddForce(new Vector2(0f, jumpForce));
         }
 
     }
 
     void Update()
     {
         grounded = Physics2D.Linecast(transform.position, GroundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
         
         if (Input.GetButtonDown("Jump") && grounded)
         {
             jump = true;
             anim.SetTrigger("Jump");
         }
     }
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

This is his:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
 
     [HideInInspector] public bool facingRight = true;
     [HideInInspector] public bool jump = false;
     public float moveForce = 365f;
     public float maxSpeed = 5f;
     public float jumpForce = 1000f;
     public Transform groundCheck;
 
 
     private bool grounded = false;
     private Animator anim;
     private Rigidbody2D rb2d;
 
 
     // Use this for initialization
     void Awake()
     {
         anim = GetComponent<Animator>();
         rb2d = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
 
         if (Input.GetButtonDown("Jump") && grounded)
         {
             jump = true;
         }
     }
 
     void FixedUpdate()
     {
         float h = Input.GetAxis("Horizontal");
 
         anim.SetFloat("Speed", Mathf.Abs(h));
 
         if (h * rb2d.velocity.x < maxSpeed)
             rb2d.AddForce(Vector2.right * h * moveForce);
 
         if (Mathf.Abs(rb2d.velocity.x) > maxSpeed)
             rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
 
         if (h > 0 && !facingRight)
             Flip();
         else if (h < 0 && facingRight)
             Flip();
 
         if (jump)
         {
             anim.SetTrigger("Jump");
             rb2d.AddForce(new Vector2(0f, jumpForce));
             jump = false;
         }
     }
 
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

I know the issue is going to be the most stupid thing, like I missed a semi-colon or something but I really can't see what I've done wrong so I need a few extra eyes with fresh perspective.

Thanks a lot for helping.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by peaceofmind1188 · Sep 06, 2018 at 04:56 AM

Did you check your layer collisions? Under edit, project settings, physics 2d? I used this extensively in my app I just published. You can check it out, it's called Microbz:

https://itunes.apple.com/us/app/microbz/id1418937151?ls=1&mt=8 https://play.google.com/store/apps/details?id=com.peaceofmindgames.microbz

You can see quite a bit of collision work, most of the magic was done using the layer collisions. Hope this was your problem.

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 surfuay · May 10, 2019 at 12:52 AM 0
Share

slick game, kudos

avatar image
0

Answer by Atiyeh123 · Sep 05, 2018 at 07:48 PM

Hi, What happens if you disable PlayerController code cause i think problem isn't from the code. check two things. 1.there is no tick on any collider IsTriggers. 2.both platform and character has same z.(if your game is 3d).

Comment
Add comment · Show 2 · 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 Vane7625 · Sep 06, 2018 at 03:44 AM 0
Share

I checked what you told me to. The IsTrigger box is not ticked on the character nor on the platform. And I tried the z position thing just to be sure but it doesn't affect at all because my game is 2d, not 3d. Now onto the code advice you gave me: I concluded immediately that the problem was the code because on the same game, same project, same everything, I put $$anonymous$$e in and the player fell, and when I replaced it with his the player behaved as it should. So I don't think taking out the code will help any. But I do have some things to report. I noticed (after some sleep and very-needed fresh perspective) that I had two mistakes on my script, the first is that I initialized the jump variable as true and he initialized as false. The second was that I never set jump to false after the character jumped. Today when I ran my code again the issue was that the character kept jumping infinately, the y position values were positive. I changed both things and everything behaved as it should. But now I'm wondering why the character was falling yesterday. Because it was falling, I know it, the y position values were negative. I think maybe it was a bug or glitch of some type but I can't be sure enough to report it if it doesn't happen again. Has this happened to anyone else?

avatar image Dankey_Kang Vane7625 · May 10, 2019 at 01:19 AM 0
Share

Out of curiosity, what value did you have jumpForce set to?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

106 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

Related Questions

How to create soil or sand?? 1 Answer

Unity crashes 0 Answers

Character gets stuck on projectile, despite different layers,Player gets stuck on projectile, despite different layers 0 Answers

HOLD THE JUMP BUTTON TO JUMP HIGHER 0 Answers

RigidBody2d not detecting collision when dragged 1 Answer


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