Home > Back-end >  Jump is not moving the character on Y axis
Jump is not moving the character on Y axis

Time:11-03

The debug log for the jump function shows up in the console correctly, but I can't find any way to write the code that actually makes the character move up on the Y axis when I press the button.

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

public class KittenController : MonoBehaviour
{
    Rigidbody2D rigidbody2d;
    Animator animator;

    public float speed;
    public float jumpForce;

    private float idleTimer;

    float horizontal;
    float vertical;
    //int direction;

    Vector2 lookDirection = new Vector2(1, 0);
    Vector2 jumping = new Vector2(0, 100);

    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        idleTimer = 0;
    }

    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        // vertical = Input.GetAxis("Vertical");

        Vector2 move = new Vector2(horizontal, vertical);

        // Jump Movement
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Jump();
            Debug.Log("Space Pressed");
            // animator.SetTrigger("Jump");
        }

        if (move.magnitude == 0)
        {
            idleTimer  = Time.deltaTime;
            //Debug.Log("Idle Time is : "   idleTimer);
        }
        else
        {
            idleTimer = 0;
        }

        if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
        {
            lookDirection.Set(move.x, move.y);
            lookDirection.Normalize();
            //Debug.Log("X is: "   lookDirection);
        }

        //animator.SetFloat("Look X", lookDirection.x);
        animator.SetFloat("Move X", lookDirection.x);
        animator.SetFloat("Speed", move.magnitude);

    }
    
    void FixedUpdate()
    {
        Vector2 position = rigidbody2d.position;

        position.x = position.x   speed * horizontal * Time.deltaTime;
        position.y = position.y   speed * vertical * Time.deltaTime;

        rigidbody2d.MovePosition(position);
    }

    void Jump()
    {
        //rigidbody2d.AddForce(jumping, ForceMode2D.Impulse);
        //rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, jumpForce);
        rigidbody2d.velocity = Vector2.up * jumpForce;

        Debug.Log("Jumping");
    }
}

CodePudding user response:

You maybe jumping, but rightafter, almost at the same time you are setting the y position with the rigidbody2d.MovePosition(position); in the FixedUpdate()

First dont use the fixed update if there is specific need of a constant delta time (physics or a maybe a replay system). You should use the Update() instead.

Second, usually handling movement setting the transform with code and also with physics (setting a velocity to the rigidbody) is not a good idea. You need to choose wether your object moves with physics or to set the transform.position in the code (maybe applying physics also, but coded by yourself). Take into account that if you set a transform.position directly in the code, very probably won't make sense with the physics calculations if the object is moving with physics, so unexpected results will take place.

I would remove all the code in the FixedUpadte() and most of the code, and try just to work out that the jump works, with rigidbody2d.velocity = Vector2.up * jumpForce; or with rigidbody2d.AddForce(new Vector2(0f, 1.0f), ForceMode2D.Impulse). The I would try to work out the movement separatedly, maybe with rigidbody2d.AddForce(new Vector2(1.0f, 0f), ForceMode2D.Force). Then maybe try to work them out together for the respective inputs.

I you want to handle the position setting the transform.position directly instead of using physics, I would also split the problem into smaller ones, first achieve movement maybe with

position.x = position.x   speed * horizontal * Time.deltaTime;

And then the jump. For the position.y you may to apply uniformly accelerated motion physics and apply that logic to your vertical position when you jump, or maybe figure a type of jump of your own with a movement you define.

  • Related