My Sprite is Moving against a platform.

My Sprite is Moving against a platform.

I have a script and an unusual one (that's what one of the member said.) My isGrounded isn't using any rayCasting. I didn't have any problem with it but when I created a platform and jump against it:

  1. My character would fall down slowly.
  2. If I try to move against the platform, I would be able to move and it would appear as if the character was flying.

So question is, how can I stop all my movements, jumping everything, when I collide like this with a platform?

My Code is:

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

public class pScript : MonoBehaviour {
    public Rigidbody2D rb;
    public Transform[] Points;
    public float radius;
    public LayerMask layer;
    public bool jump;
    public bool isgrounded = false;
    public float MoveSpeed;
    [SerializeField ]
    public float jumpHigh;
    public bool facingRight;
    public Animator animator;
    public Object CallfPanel;
    public Object CallfText;
    public float Move = 8f;
    public Vector3 Dir;
    public Vector3 tart;
    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody2D> ();
        animator = GetComponent<Animator> ();
        CallfPanel = GameObject.Find ("Panel");
        CallfText = GameObject.Find ("Text");

    // Update is called once per frame
    void Update () {
        RaycastHit hit;
        Debug.DrawRay (transform.position, Vector3.right, Color.white, Move);
        tart = new Vector3 (200, 300, transform.position.z);
        Dir = Vector3.right;
        if (jump == true) {
            if (Physics.Raycast (tart, Dir, out hit, Move)) {
                jump = false;

        float horizontal = Input.GetAxis ("Horizontal");
        animator.SetFloat ("speed", Mathf.Abs (horizontal));

        isgrounded = isGrounded ();
        rb.velocity = new Vector2 (horizontal * MoveSpeed, rb.velocity.y);
        if (Input.GetKeyDown (KeyCode.Space)) {
            jump = true;
        if (isgrounded && jump == true) {
            isgrounded = false;
            rb.velocity = new Vector2 (0, jumpHigh);
        if (isgrounded == false) {
            jump = false;
        changeDirections ();
    private bool isGrounded(){

        if (rb.velocity.y <= 0) {
            foreach (Transform points in Points) {
                Collider2D[] collider = Physics2D.OverlapCircleAll (points.position, radius, layer);
                for (int i = 0; i < collider.Length; i++) {
                    if (collider [i].gameObject != gameObject) {
                        return true;



        }return false;
    public void changeDirections(){
        float horizontal = Input.GetAxis("Horizontal");
        Vector3 theScale = transform.localScale;
        if (theScale.x == 1) {
            facingRight = true;
        } else {
            facingRight = false;

        if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) {
            theScale.x *= -1;
            transform.localScale = theScale;
    public void showTextDuration(){
        float lifetime = 4.0f;
        Destroy (CallfPanel, lifetime);


I'm not sure about how exactly to do this in Unity rigid body physics but this is how I would do it in a conventional approach.

Create a collision detection function for four sides then replace isGrounded with bool collisions[4], and isGrounded() with your new collision detection function.

Instead of what you're doing now, check for collisions after the player moves and if the character is inside of an object, eject him to the edge of that object in the direction the player came from. Make sure the character is never inside the object when the frame ends.

Also, I don't use Unity, but wouldn't it be simpler to use more conventional methods to create a platformer? Is there a specific reason you have to make it harder on yourself and use rigid body physics?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.