Gravity help?

I didn’t really know what else to title this, but I’m making a 2d side scrolling game and I’m currently trying to figure out how to make it so when the character jumps, he will fall back down and hit the ground. I have created my code in C#, so when I run the game, the block just stays in the air and doesn’t fall, what am I doing wrong? I am new at this, so don’t hate on me too much. Here is the code so far:

Here is the Controller2d tab

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (BoxCollider2D))]
public class Controller2d : MonoBehaviour {

    public LayerMask collisonMask;

    const float skinWidth= 0.015f;
    public int horizontalRayCount = 4;
    public int verticalRayCount = 4;

    float horizontalRaySpacing;
    float verticalRaySpacing;
   
    RaycastOrigins raycastOrigins;

    void Start() {
        BoxCollider2D collider;
        collider = GetComponent<BoxCollider2D> ();
        CalculateRaySpacing ();
    }

    public void Move(Vector3 velocity) {
        UpdateRaycastOrigins();

        VerticalCollisons (ref velocity);


        transform.Translate (velocity);
    }

    void VerticalCollisons(ref Vector3 velocity) {
        float directionY = Mathf.Sign (velocity.y);
        float rayLength = Mathf.Abs (velocity.y) + skinWidth;

        for (int i = 0; i < verticalRayCount; i ++) {
            Vector2 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft : raycastOrigins.topLeft;
            rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
            RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.up * directionY, rayLength, collisonMask);
           
            Debug.DrawRay (raycastOrigins.bottomLeft + Vector2.right * verticalRaySpacing * i, Vector2.up * -2, Color.blue);
           
            if (hit) {
                velocity.y = (hit.distance - skinWidth) * directionY;
                rayLength = hit.distance;

            }
        }
    }

    void UpdateRaycastOrigins() {
        Bounds bounds = GetComponent<Collider>().bounds;
        bounds.Expand (skinWidth * -2);

        raycastOrigins.bottomLeft = new Vector2 (bounds.min.x, bounds.min.y);
        raycastOrigins.bottomRight = new Vector2 (bounds.max.x, bounds.min.y);
        raycastOrigins.topLeft = new Vector2 (bounds.min.x, bounds.max.y);
        raycastOrigins.topRight = new Vector2 (bounds.max.x, bounds.max.y);

    }

    void CalculateRaySpacing() {
        Bounds bounds = GetComponent<Collider>().bounds;
        bounds.Expand (skinWidth * -2);

        horizontalRayCount = Mathf.Clamp (horizontalRayCount, 2, int.MaxValue);
        verticalRayCount = Mathf.Clamp (verticalRayCount, 2, int.MaxValue);

        horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
        verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
    }

    struct RaycastOrigins {
        public Vector2 topLeft, topRight;
        public Vector2 bottomLeft, bottomRight;
    }
}

Here is the Player tab

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Controller2d))]
public class Player : MonoBehaviour {

    float gravity = -20;
    Vector3 velocity;

    Controller2d controller;

    void Start() {
        controller = GetComponent<Controller2d> ();
    }

    void Update() {
        velocity.y += gravity * Time.deltaTime;
        controller.Move (velocity * Time.deltaTime);
    }
}

I have created a new Layer named Obstacle and made the collision mark on the player set to “Obstacle”

P.S. Since I am new and want to learn, please don’t just give me the answer, instead, give me a hint on what I should look for or change

Start by putting a breakpoint in where you are applying the gravity & try debugging it. See if it is going to that line, what happens when it does, & what happens after.

What do you mean a breakpoint?

On your script, click run, attach & choose unity editor. Scroll down to the line & click in the left margin, a dot should appear at that line. This is a breakpoint. When you run the game in unity the code will execute & stop when it gets to that line. You can then step thru the code line by line & hovering the mouse will show you the values at each point. There may be a video somewhere that shows how it works. Debugging can be painful but is useful to know as you will likely be using it often.

1 Like

Alright, thanks! I will check it out tomorrow