Home > front end >  My character does not jump after doing as suggested in my previous question
My character does not jump after doing as suggested in my previous question

Time:06-03

My character doesn't jump after I tried a fix suggested to me in my previous question linked below. It only moves left and right and if i remove what i changed it doesn't move at allhttps://stackoverflow.com/questions/72474911/why-does-my-character-stop-moving-after-changig-a-0-to-1

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

public class Player : MonoBehaviour
{
[SerializeField] private Transform groundCheckTransform = null;
private bool jumpKeyWasPressed;
private float horizontalInput;
private Rigidbody rigidbodyComponent;


// Start is called before the first frame update
void Start()
{
    rigidbodyComponent = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{

    if (Input.GetKeyDown(KeyCode.W))
    {
        jumpKeyWasPressed = true;
    }

    horizontalInput = Input.GetAxis("Horizontal");
}

//Fixed update is called every physics update
private void FixedUpdate()
{
    rigidbodyComponent.velocity = new Vector3(horizontalInput,
        rigidbodyComponent.velocity.y, 0);
    if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f).Length == 1)
    {
        return;
    }
   

    if (jumpKeyWasPressed)
    {

        rigidbodyComponent.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
        jumpKeyWasPressed = false;
    }

    rigidbodyComponent.velocity = new Vector3(horizontalInput, 
rigidbodyComponent.velocity.y, 0);
}


}

CodePudding user response:

I think there is no need to use same code twice in the beginning and the end of fixed update block. It might not be the problem but maybe your addForce is not working because you define new values for same variables in the same cycle.

CodePudding user response:

I think this is an issue with the ground check if statement (if (Physics.OverlapSphere...)

I'm not really familiar with that use of Physics.OverlapSphere, and I don't know what the .Length is supposed to return, but I suspect the following:

Your ground check is testing to see if the character is on the ground, and then only running return; which then skips the rest of the code, thus never running the jump code if the player is on the ground? Maybe you need to put the jump code into the groundcheck if statement, like this:

if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f).Length == 1)
    {
        if (jumpKeyWasPressed)
        {

            rigidbodyComponent.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
            jumpKeyWasPressed = false;
        }
    }

Remember return; immediately stops the function, and returns its value or no value. All the code after the return; will be skipped.

Maybe use Debug.Log() to send messages to console to debug this more. If you put one in the ground check if statement, you can see when it is succeeding and when it is failing (if it fails then the jump if statement will run, so if it always succeeds then the jump statement will always be skipped.)

Feel free to ask me to clarify if that was confusing.

CodePudding user response:

Maybe you could give some more information about the settings of your RigidBody. Is the setting "isKinematic" true? If thats the case, try to use another setting.

Did you debug your code and could make sure that it is not returning before applying the force?

I would recommend that you only calculate the velocity (first line in FixedUpdate) if you need to.

  • Related