Platformer Script Error

Hi all. I’m working on a 2.5D platformer, and I was using this tutorial as a guide for left and right movement.

Down below is the script, which I’m pretty sure is the same as the video’s script, except for Vector3’s instead of Vector2’s:

public class SCPlayerControls : MonoBehaviour
{
   
    public float movementSpeed;
    public Rigidbody SC;
  
    float mx;

    private void Update()
    {
        mx = Input.GetAxisRaw("Horizontal");
    }
   
    private void FixedUpdate()
    {
        Vector3 movement = new Vector3 (mx = movementSpeed, SC.velocity.y);

        SC.velocity = movement;
    }
}

However, when I press start, the placeholder I was using kept moving to the right, and none of the buttons worked. Is this a problem on my part that I need to use Debug.Log() for, or should I just find a different script?

You used mx = movementSpeed, I assume you meant mx * movementSpeed.

Yeah, that was it. Thanks for the help.

UPDATE: I was able to make the the left-right movement alright, but I’m now struggling with the jumping physics.

public class SCPlayerControls : MonoBehaviour
{

    public float movementSpeed;
    public Rigidbody SC;
    public float jumpForce = 20f;
    public Transform feet;
    public LayerMask groundLayers;

    float mx;
    private void Update()
    {
        mx = Input.GetAxisRaw("Horizontal");

        if (Input.GetButtonDown("Jump") && isGrounded())
        {
            Jump();
            Debug.Log("isGrounded");
            Debug.Log("Jump");
        }
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(mx * movementSpeed, SC.velocity.y);

        SC.velocity = movement;
    }

    void Jump()
    {
        Vector2 movement = new Vector2(SC.velocity.x, jumpForce);

        SC.velocity = movement;
    }
    public bool isGrounded()
    {
        Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);

        if (groundCheck != null)
        {
            return true;
        }
        return false;
    }

I was using Muddy Wolf’s jumping tutorial* , and the Debug.Log()'s are to track the code in the if statement. However, pressing the jump key outright does nothing to move the player character up or log anything in the console.
Is there a way to fix this?

First, I would check and see if your isGrounded() check is actually working.

Alright, so I redid the code, and got the same results. No jumping, and no message in the console. Is there any other place I need to put the Debug.Log’s so I can see what’s going on?

Do you mean to say that you followed @kdgalla 's advice:

and printed out isGrounded to see if it is true or false, and nothing printed on the console?

This just means you are not even running this code. Find out why.

Well, that’s the issue I’m having. I followed the tutorial, but it won’t jump like in the tutorial. The only thing I can come up with is maybe it’s my version of Unity being different from the one in the video or me using a 2D tutorial for a 2.5D game.

If you can’t see the results of your Debug.Log statements then that means the body of your if statement is not being entered. So if you want to see a Debug.Log then you need to put it OUTSIDE of your if statement.

For example, change your Update to

  private void Update()
    {
        mx = Input.GetAxisRaw("Horizontal");
        Debug.Log("isGrounded returns "+isGrounded());
        if (Input.GetButtonDown("Jump") && isGrounded())
        {
            Jump();
            Debug.Log("isGrounded");
            Debug.Log("Jump");
        }
    }

Ok, I see what you mean. I ran the code, and apparently “isGrounded” is always false. I’ll probably find a way to fix that on my own, though.

Several things can go wrong. Make sure that feet.position is where it should be. Make sure your ground is on the same layer that you have in your layer mask. Make sure that the 0.5 radius is reasonable for your over-all scale. Also remember that Overlap circle won’t work on 3D colliders.

I actually tried replacing Collider2D and Physics2D with Collider and Physics and OverlapSphere, but I just got an error saying “Cannot implicitly convert type UnityEngine.Collider to UnityEngineCollider[ ]”. Is there a way to fix that?

Overlap sphere returns an array of colliders instead of a collider. So instead of
Collider groundCheck;

you’d declare it like
Collider[ ] groundCheck;

Look up C# arrays if you don’t know what that is. Basically instead of a Collider, it’s like a numbered group of colliders.

But, of course, OverlapSphere doesn’t work on 2D colliders.

UPDATE: Good news and bad news. The good news is that I was able to make my character jump by using the brackets on the normal Collider. The bad news is that now “isGrounded” doesn’t seem to have any bearing on how the player character jumps, which means it can jump infinitely.

Sounds like you have the reverse problem now. Instead of never detecting collisions, now its detecting collisions when it’s not supposed to. Your IsGrounded method is probably detecting with a collision with something it’s not supposed to, like possibly the character itself. You can loop through the colliders in groundCheck and debug.log the names of the GameObjects to see if there is anything unexpected.