New coder, not sure why this tutorial (hippity hoppity code my property) not working.

im not sure if its the CODE or the Inspectors Components.

The tutorial i used:
Unity 2D Character Controller Tutorial - Roystan

The code: (its pretty long)

using UnityEngine;

[RequireComponent(typeof(BoxCollider2D))]
public class PlayerMovement : MonoBehaviour
{
    [SerializeField, Tooltip("Max speed, in units per second, that the character moves.")]
    float speed = 9;

    [SerializeField, Tooltip("Acceleration while grounded.")]
    float walkAcceleration = 75;

    [SerializeField, Tooltip("Acceleration while in the air.")]
    float airAcceleration = 30;

    [SerializeField, Tooltip("Deceleration applied when character is grounded and not attempting to move.")]
    float groundDeceleration = 70;

    [SerializeField, Tooltip("Max height the character will jump regardless of gravity")]
    float jumpHeight = 4;

    private BoxCollider2D boxCollider;

    private Vector2 velocity;

    /// <summary>
    /// Set to true when the character intersects a collider beneath
    /// them in the previous frame.
    /// </summary>
    private bool grounded;

    private void Awake()
    {     
        boxCollider = GetComponent<BoxCollider2D>();
    }

    private void Update()
    {
        // Use GetAxisRaw to ensure our input is either 0, 1 or -1.
        float moveInput = Input.GetAxisRaw("Horizontal");

        if (grounded)
        {
            velocity.y = 0;

            if (Input.GetButtonDown("Jump"))
            {
                // Calculate the velocity required to achieve the target jump height.
                velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y));
            }
        }

        float acceleration = grounded ? walkAcceleration : airAcceleration;
        float deceleration = grounded ? groundDeceleration : 0;

        if (moveInput != 0)
        {
            velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, acceleration * Time.deltaTime);
        }
        else
        {
            velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime);
        }

        velocity.y += Physics2D.gravity.y * Time.deltaTime;

        transform.Translate(velocity * Time.deltaTime);

        grounded = false;

        // Retrieve all colliders we have intersected after velocity has been applied.
        Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0);

        foreach (Collider2D hit in hits)
        {
            // Ignore our own collider.
            if (hit == boxCollider)
                continue;

            ColliderDistance2D colliderDistance = hit.Distance(boxCollider);

            // Ensure that we are still overlapping this collider.
            // The overlap may no longer exist due to another intersected collider
            // pushing us out of this one.
            if (colliderDistance.isOverlapped)
            {
                transform.Translate(colliderDistance.pointA - colliderDistance.pointB);

                // If we intersect an object beneath us, set grounded to true.
                if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0)
                {
                    grounded = true;
                }
            }
        }
    }
}

When i touch the floor, it teleports me back up, making it so i cant jump, and it just doesnt look good too.

on the player, i have a sprite renderer, transform, box collider, (no rigid body because i cant have one with the code im using.)

since im new, dunno how to fix it, and theres no tutorials on how to fix this issue… im going here. please help!

Hi again. I’m glad you’re continuing your Unity adventure.

I have to give you pretty much the same advice as last time. The script you have there isn’t exactly beginner-friendly in my opinion, and the script alone is likely not enough to solve your problem in this case. I doubt anyone in the forums will have the patience to go through that script for you. The tutorial to fix that issue is the tutorial you got the code from.

If you just want to experiment with controlling a 2D character, there are more simple ways to do it. But do yourself a favor and don’t just copy-paste something from somewhere. You won’t learn that way. You can always switch to a more advanced controller once you know better what you need.

If you just want to move a sprite around to test things, there’s no need to do things in a very complex way. Try this, for example?

I send this video to everyone starting out with basic platform movement. I agree with midsummer, use something much easier to understand. The snippet i took of your code has too much calculations which just arent necessary starting out.

Brackey’s tutorial has great fundamentals, easy to follow, and more importantly, easy to build upon with animations, sound, etc.

1 Like

Ive already tried that tutorial, simply doesnt work.
Also for brackeys.

Well the tutorials do work as they have thousands of views and many positive comments. If you are trying to add in their code to yours then it will be very difficult to work, but if you start from scratch it should work very easily. Regardless, just try out other basic 2D movement tutorials, many of them work but in different ways. To re-emphasize my and midsommers’s point from before, strip it down and do the basics, dont try to get the most optimized code and fancy code possible. You won’t learn from that as a beginner.