Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 strongo10 · May 06 at 03:08 PM · playergroundfalling-through-floorfalling-through-terrain

How am I supposed to make sure that the player can fall through the ground?

alt text

schermafbeelding-2022-05-06-om-150945.png (212.0 kB)
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
0
Best Answer

Answer by Caeser_21 · May 06 at 04:18 PM

1.Your Player needs to have a RigidBody2D
2. If the player already has a RigidBody2D, then it seems like the Background(In the Tilemap) has a TileMapCollider2D If you remove it then the player should fall through...

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 swanne · May 06 at 03:40 PM

Using Rigidbody2D and colliders is one way it could be achieved

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 strongo10 · May 06 at 04:16 PM 0
Share

Hi, thanks for the answer, I'll check it later!

avatar image
0

Answer by strongo10 · May 07 at 02:14 PM

Does it have something to do with my code?

using UnityEngine;

public class PlayerMovement : MonoBehaviour { [Header("Movement Parameters")] [SerializeField] private float speed; [SerializeField] private float jumpPower;

 [Header("Coyote Time")]
 [SerializeField] private float coyoteTime; //How much time the player can hang in the air before jumping
 private float coyoteCounter; //How much time passed since the player ran off the edge

 [Header("Multiple Jumps")]
 [SerializeField] private int extraJumps;
 private int jumpCounter;

 [Header("Wall Jumping")]
 [SerializeField] private float wallJumpX; //Horizontal wall jump force
 [SerializeField] private float wallJumpY; //Vertical wall jump force

 [Header("Layers")]
 [SerializeField] private LayerMask groundLayer;
 [SerializeField] private LayerMask wallLayer;

 [Header("Sounds")]
 [SerializeField] private AudioClip jumpSound;

 private Rigidbody2D body;
 private Animator anim;
 private BoxCollider2D boxCollider;
 private float wallJumpCooldown;
 private float horizontalInput;

 private void Awake()
 {
     //Grab references for rigidbody and animator from object
     body = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
     boxCollider = GetComponent<BoxCollider2D>();
 }

 private void Update()
 {
     horizontalInput = Input.GetAxis("Horizontal");

     //Flip player when moving left-right
     if (horizontalInput > 0.01f)
         transform.localScale = Vector3.one;
     else if (horizontalInput < -0.01f)
         transform.localScale = new Vector3(-1, 1, 1);

     //Set animator parameters
     anim.SetBool("run", horizontalInput != 0);
     anim.SetBool("grounded", isGrounded());

     //Jump
     if (Input.GetKeyDown(KeyCode.Space))
         Jump();

     //Adjustable jump height
     if (Input.GetKeyUp(KeyCode.Space) && body.velocity.y > 0)
         body.velocity = new Vector2(body.velocity.x, body.velocity.y / 2);

     if (onWall())
     {
         body.gravityScale = 0;
         body.velocity = Vector2.zero;
     }
     else
     {
         body.gravityScale = 7;
         body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);

         if (isGrounded())
         {
             coyoteCounter = coyoteTime; //Reset coyote counter when on the ground
             jumpCounter = extraJumps; //Reset jump counter to extra jump value
         }
         else
             coyoteCounter -= Time.deltaTime; //Start decreasing coyote counter when not on the ground
     }
 }

 private void Jump()
 {
     if (coyoteCounter <= 0 && !onWall() && jumpCounter <= 0) return; 
     //If coyote counter is 0 or less and not on the wall and don't have any extra jumps don't do anything

     SoundManager.instance.PlaySound(jumpSound);

     if (onWall())
         WallJump();
     else
     {
         if (isGrounded())
             body.velocity = new Vector2(body.velocity.x, jumpPower);
         else
         {
             //If not on the ground and coyote counter bigger than 0 do a normal jump
             if (coyoteCounter > 0)
                 body.velocity = new Vector2(body.velocity.x, jumpPower);
             else
             {
                 if (jumpCounter > 0) //If we have extra jumps then jump and decrease the jump counter
                 {
                     body.velocity = new Vector2(body.velocity.x, jumpPower);
                     jumpCounter--;
                 }
             }
         }

         //Reset coyote counter to 0 to avoid double jumps
         coyoteCounter = 0;
     }
 }

 private void WallJump()
 {
     body.AddForce(new Vector2(-Mathf.Sign(transform.localScale.x) * wallJumpX, wallJumpY));
     wallJumpCooldown = 0;
 }


 private bool isGrounded()
 {
     RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
     return raycastHit.collider != null;
 }
 private bool onWall()
 {
     RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
     return raycastHit.collider != null;
 }
 public bool canAttack()
 {
     return horizontalInput == 0 && isGrounded() && !onWall();
 }

}

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 Caeser_21 · May 07 at 03:09 PM 0
Share

Your player can move when he is on the ground right? If so than I'm pretty sure the only issue should be with the Rigidbody2D...

avatar image Caeser_21 · May 07 at 03:12 PM 0
Share

Also could you explain how uour game mechanics work? It would be helpful to change the script to make it cleaner...

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

211 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

My characters can't sto falling terrain 1 Answer

Help with falling rocks 3 Answers

how to tell what platform you're on. 1 Answer

Player Falls Through The Ground 1 Answer

On Unity, after I add the CharacterController and JavaScript, I keep falling through the cube floor i made, no matter how thick it is! Can someone help me? 2 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