Kylee
Kylee

Reputation: 1675

Movement really slow in Unity

I'm working on a very basic side scrolling platformer in Unity 3D. The player is a cube, you can move left and right, you can jump. I can't get the movement correct as the character moves too slow.

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

public class Player : MonoBehaviour
{
    public float walkSpeed = 15f;
    public float jumpSpeed = 15f;
    public float gravity = 50f;
    
    Rigidbody rb;

    bool pressedJump = false;

    Vector3 movementInput;
    
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        movementInput = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    }
    
    void FixedUpdate()
    {
        WalkHandler();
        
        JumpHandler();

        GravityHandler();
    }
    
    void WalkHandler()
    {
        rb.velocity = new Vector2(0f, rb.velocity.y);

        rb.velocity = movementInput * walkSpeed * Time.fixedDeltaTime;
    }
    
    void JumpHandler()
    {
        if (Input.GetButtonDown("Jump"))
        {
            if (!pressedJump)
            {
                pressedJump = true;
                
                Vector3 jumpVector = new Vector3(0f, jumpSpeed, 0f);
                
                rb.velocity = rb.velocity + jumpVector;
            }
        }
        else
        {
            pressedJump = false;
        }
    }

    void GravityHandler()
    {
       rb.AddForce(Vector3.down * gravity * rb.mass);
    }
}

More context I originally had this implemented and it worked. Very smooth movement and jumping. I tragically lost that game due to my own actions. So I'm trying to recreate it. Here is a video of the original movement and jumping I'm looking to recreate:

My Progress so Far

Note: The values for walkSpeed, jumpForce and gravity were what was used previously.

Losing the first game was pretty soul crushing so any help would be great.

Upvotes: 0

Views: 1988

Answers (1)

Kylee
Kylee

Reputation: 1675

OK so I'm not sure how to recreate this using velocity but this seems to work:

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

public class Player : MonoBehaviour
{
    public float walkSpeed = 15f;
    public float jumpSpeed = 15f;
    public float gravity = 50f;
    
    Rigidbody rb;

    bool pressedJump = false;

    float hAxis;
    
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        hAxis = Input.GetAxisRaw("Horizontal");
    }
    
    void FixedUpdate()
    {
        WalkHandler();
        
        JumpHandler();

        GravityHandler();
    }
    
    void WalkHandler()
    {
        rb.velocity = new Vector2(0f, rb.velocity.y);
        
        Vector3 movement = new Vector2(hAxis * walkSpeed * Time.deltaTime, 0f);
        
        rb.MovePosition(transform.position + movement);
    }
    
    void JumpHandler()
    {
        if (Input.GetButtonDown("Jump"))
        {
            if (!pressedJump)
            {
                pressedJump = true;

                Vector3 jumpVector = new Vector3(0f, jumpSpeed, 0f);

                rb.velocity = rb.velocity + jumpVector;
            }
        }
        else
        {
            pressedJump = false;
        }
    }

    // Check if the object is grounded
    bool CheckGrounded()
    {
        return true;
    }

    void GravityHandler()
    {
       rb.AddForce(Vector3.down * gravity * rb.mass);
    }
}

Ignore the CheckGrounded method

Upvotes: 0

Related Questions