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.
Your answer
Follow this Question
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