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 /
  • Help Room /
avatar image
0
Question by PolymathicIndustries · Jun 14, 2020 at 04:54 PM · rigidbody2dgravityphysics2dcollider2dplatformer

Had to turn off gravity scale, but now player goes through walls

I am a beginner trying to learn to make a basic platformer to start. I made characters in Gimp and exported them, did the same for the background pieces. I did not use a tilemap as I wasn't able to paste my ground block into the tilemap to paint so I just added them to the scene to test the concept. I have a simple player on a simple ground. What is listed below is the code I found that allows me to move my character:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Platformer : MonoBehaviour { Rigidbody2D rb;

 public float speed;
 public float jumpForce;

 public float fallMultiplier = 2.5f;
 public float lowJumpMultiplier = 2f;

 bool isGrounded = false;
 public Transform isGroundedChecker;
 public float checkGroundRadius;
 public LayerMask groundLayer;

 public float rememberGroundedFor;
 float lastTimeGrounded;

 public int defaultAdditionalJumps = 1;
 int additionalJumps;



 void Start()
 {
     rb = GetComponent<Rigidbody2D>();

     additionalJumps = defaultAdditionalJumps;
 }

 void Update()
 {
     Move();
     Jump();
     BetterJump();
     CheckIfGrounded();
 }


 void Move() {
     float x = Input.GetAxisRaw("Horizontal");

     float moveBy = x * speed;

     rb.velocity = new Vector2(moveBy, rb.velocity.y);
 }

 void Jump() {
     if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundedFor || additionalJumps > 0)) {
         rb.velocity = new Vector2(rb.velocity.x, jumpForce);
         additionalJumps--;
     }
 }

 void BetterJump() {
     if (rb.velocity.y < 0) {
         rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
     } else if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space)) {
         rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
     }   
 }

 void CheckIfGrounded() {
     Collider2D colliders = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);

     if (colliders != null) {
         isGrounded = true;
         additionalJumps = defaultAdditionalJumps;
     } else {
         if (isGrounded) {
             lastTimeGrounded = Time.time;
         }
         isGrounded = false;
     }
 }

}

Now, I have added a rigidbody 2d to my player as well as a capsule collider and I have added box colliders to all of the ground blocks. As soon as I hit Play the character will slide through these blocks and fall off the screen. I Googled this and found that changing the gravity scale can solve this issue so i changed my gravity scale to 0 from 1 and then suddenly I could move my character left to right (through no jump function), but now he moves through walls. Can anyone tell me what I am doing wrong? I have been researching and trying to find my answers, but I have been unable to figure this one out.

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

My Unity Rigidbody 2D character fall slowly after a jump,Unity RigidBody Character falls slowly 0 Answers

2D Physics not working correctly 1 Answer

A box collider 2D (Is Trigger marked) stops my player from moving which has rigidbody 2D 1 Answer

how to fix velocity for jump and movement or fall in 2d pltform game with gravity 4 Answers

Different rigidbody2D not working together? 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