- Home /
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.
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.
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).
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?
Out of curiosity, what value did you have jumpForce set to?