Forum begins after the advertisement:


[Part 1] Jump is not working

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 1] Jump is not working

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #18885
    samuele marcelli
    Level 1
    Participant
    Helpful?
    Up
    0
    ::
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Settings")]
        private Rigidbody2D rb;
       [SerializeField] private float walkSpeed = 1;
        private float xAxis;
    
        private float jumpForce = 45;
    
        // Start is called before the first frame update
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
        }
    
        // Update is called once per frame
        void Update()
        {
            GetInputs();
            Move();
        }
    
        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        private void Move()
        {
            rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y);
        }
    }
    #18886
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Hello, samuele marcelli! It looks like you’re working on a player controller script in Unity. I see that you have a basic setup for horizontal movement. If you’re facing any issues or have specific questions about this code, feel free to ask!

    Here are a few things you might want to consider or check:

    1. Jumping Mechanic: You have a jumpForce variable defined, but it looks like you haven’t implemented the jumping logic yet. If you want to add jumping, you could check for a jump input (like the space bar) and apply a force to the Rigidbody2D.

    2. Ground Check: To prevent the player from jumping while in the air, you’ll need to implement a ground check. This can be done using raycasting or collision detection.

    3. Serialized Fields: If you want to adjust the walkSpeed and jumpForce from the Unity Inspector, make sure to mark them as [SerializeField].

    4. Code Formatting: It seems like there are some HTML entities in your code (like < for <). Make sure your code is clean and properly formatted to avoid syntax errors.

    Here's a quick example of how you might implement jumping:

    private bool isGrounded;
    
    void Update()
    {
        GetInputs();
        Move();
        Jump();
    }
    
    private void Jump()
    {
        if (isGrounded && Input.GetButtonDown("Jump")) // "Jump" is usually mapped to the space bar
        {
            rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
        }
    }
    
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground")) // Make sure your ground objects have the "Ground" tag
        {
            isGrounded = true;
        }
    }
    
    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }

    Make sure to tag your ground objects appropriately in Unity. If you have any other questions or need further assistance, just let me know! Happy coding!

    #18890
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    the console says:” the field PlayerController.jumpForce is assigned but its value is never used. What should i do?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Settings")]
        private Rigidbody2D rb;
       [SerializeField] private float walkSpeed = 1;
        private float xAxis;
    
        private float jumpForce = 45;
    
        // Start is called before the first frame update
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
        }
    
        // Update is called once per frame
        void Update()
        {
            GetInputs();
            Move();
        }
    
        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        private void Move()
        {
            rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y);
        }
    }
    #18892
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    The warning you are seeing indicates that the jumpForce variable is declared and assigned a value, but it is never used in your code. If you intend to implement jumping functionality for your player character, you’ll need to use the jumpForce value to make the player jump when a specific input is detected (like pressing the space bar).

    Here’s how you can modify your PlayerController script to include jumping functionality:

    1. Update the GetInputs method to check for jump input.
    2. Add a method to handle jumping.
    3. Apply the jumpForce when the player jumps.

    Here’s an example of what the updated code might look like:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerController : MonoBehaviour
    {
        [Header("Horizontal Movement Settings")]
        private Rigidbody2D rb;
        [SerializeField] private float walkSpeed = 1;
        private float xAxis;
    
        [Header("Jump Settings")]
        private float jumpForce = 45;
        private bool isGrounded; // To check if the player is on the ground
    
        // Start is called before the first frame update
        void Start()
        {
            rb = GetComponent();
        }
    
        // Update is called once per frame
        void Update()
        {
            GetInputs();
            Move();
            Jump();
        }
    
        void GetInputs()
        {
            xAxis = Input.GetAxisRaw("Horizontal");
        }
    
        private void Move()
        {
            rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y);
        }
    
        private void Jump()
        {
            // Check if the player is grounded and jump input is pressed
            if (isGrounded && Input.GetButtonDown("Jump"))
            {
                rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
            }
        }
    
        private void OnCollisionEnter2D(Collision2D collision)
        {
            // Check if the player is touching the ground
            if (collision.gameObject.CompareTag("Ground"))
            {
                isGrounded = true;
            }
        }
    
        private void OnCollisionExit2D(Collision2D collision)
        {
            // Set isGrounded to false when leaving the ground
            if (collision.gameObject.CompareTag("Ground"))
            {
                isGrounded = false;
            }
        }
    }

    Explanation:

    1. Jump Method: The Jump method checks if the player is grounded and if the jump button (usually the space bar) is pressed. If both conditions are true, it applies an upward force to the player using AddForce.
    2. Ground Check: The OnCollisionEnter2D and OnCollisionExit2D methods are used to set the isGrounded variable based on whether the player is touching an object tagged as “Ground”. Make sure to tag your ground objects accordingly in Unity.

    This should resolve the warning about the jumpForce variable, as it will now be actively used in your code.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: