0
\$\begingroup\$

UDPATE2: I solved my jumping problem without using Input.GetAxis. The final code is at bottom. It's very simple. It uses rigidBody.transform.position with a boolean.


UPDATE: Well, despite the good suggestions from @alex-f @Alex F below, I can't seem to solve the jump problem through tweaking things about Input.GetAxis("Vertical") in the Input Manager. Unity is applying the upwards impulse even when it doesn't meet the if statement condition. I had read other people solving their jump issues through using Input.GetAxis but I'm not sure I can make it work. I think I'll try to learn a bit about raycasting and see if that helps. If anyone else has suggestions regarding GetAxis, I am happy to try them and report back behavior. I'll keep a scene for those expermiments.


Here is my code. My object shouldn't jump unless its Input.GetAxis value is <= 0.05. However, it does. If I hit the space bar key before the object's Input.GetAxis value has dropped back down, it will still apply an Impulse force in the positive y direction. The result is that the object goes up and up without returning to the ground plane.

ETA: I forgot to add another weird element. After the first jump, the word "jump" never again prints to console. However, the vertAxis DOES print to console. It doesn't print "jump" but it DOES apply a force in the y-direction.

    private bool canJump = true;
    private float vertAxis; 

    private void Update()
    {
            vertAxis = Input.GetAxis("Vertical");
            if ((canJump == false) && (!Input.GetKeyDown(KeyCode.Space)) && (vertAxis <= 0.05))
            { 
                canJump = true;
            }
    }

    private void Fixed Update()
    {       
        if(Input.GetKey(KeyCode.Space))
            {
                print(vertAxis);
                if ((vertAxis <= 0.05) && (canJump == true))
                {
                    print("jump");
                    rb.AddForce(0, y, 0, ForceMode.Impulse);
                    canJump = false;
                }
            }
    }

Updated code and more information about weird behavior. The console recognizes the pressed space key, recognizes that it has not fulfilled the if condition because it doesn't print "jump", but it STILL applies force in the positive y direction. Updated code is below. I also added integer to use in the if condition statement. I am next going to try @AlexF, @alex-f 's deadzone suggestion:

enter image description here

    private bool canJump = true;
    private float vertAxis;
    private int intAxis;

private void FixedUpdate()
{
        if(Input.GetKey(KeyCode.Space))
        {
            //vertAxis = Input.GetAxis("Vertical");
            print("keycode.space: "+ vertAxis);
            if ((intAxis == 0) && (canJump == true))
            {
                print("jump");
                rb.AddForce(0, y, 0, ForceMode.Impulse);
                canJump = false;
                print(canJump);
            }
        }
    }

    private void Update()
    {
        vertAxis = Input.GetAxis("Vertical");
        print("Update: vertAxis: " + vertAxis);
        if(vertAxis < 0.05)
        {
            intAxis = (int)vertAxis;
            print("intAxis: "+ intAxis);
            if ((canJump == false) && (!Input.GetKeyDown(KeyCode.Space)) && (intAxis == 0))
            {
                canJump = true;
                print(canJump);
            }
        }


    }

Final code (does not use Input.GetAxis - uses rigidBody.transform.position with a boolean):

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

public class testCntrllr : MonoBehaviour
{
    Rigidbody rb;
    private bool canJump = false;
    Vector3 vect3;


    void Start()
    {
        rb = GetComponent<Rigidbody>();
        vect3 = rb.transform.position;
    }

    private void FixedUpdate()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            if (canJump == true)
            {
                rb.AddForce(0, y, 0, ForceMode.Impulse);
                canJump = false;
            }


        }
    }

    private void Update()
    {
        vect3 = rb.transform.position;
        if (vect3.y <= 0.5)
        {
            canJump = true;
        }
    }

}
\$\endgroup\$
  • \$\begingroup\$ To share a solution, please post it as an Answer, not embedded in the Question. \$\endgroup\$ – DMGregory Mar 25 at 21:06
0
\$\begingroup\$

I think the root of your problem stems from the fact that you probably either want vertAxis <= -0.05 or Mathf.abs(vertAxis) <= 0.05.

What you have will do two things, which don't seem to make sense together. On the one hand, the <= 0.05 part makes it seem like you want that case to be true when the axis is released (i.e. no input). On the other hand, vertAxis <= suggests that you want the case to be true when the axis is held down (i.e. when a certain input is given). This doesn't make sense to me.

If this isn't the problem, I'm very curious to know what you want to accomplish with this code, as I can't tell from the question what behavior you're looking for (because I know some of it isn't working).

EDIT: For anyone else confused by this question, read https://docs.unity3d.com/ScriptReference/Input.GetAxis.html

\$\endgroup\$
  • \$\begingroup\$ Oh! I'm very new to using Input.GetAxis. In fact, this is my first time trying it. I thought I had it sort of figured out to the degree that I could use it for something basic like this, but now that you point that out I realize my mistake. What I REALLY want is for vertAxis = 0, no vertical movement. However, I was concerned that Unity might not be able to reliably recognize a zero float. I thought I'd just pick a reasonably small number close to zero. Let me try vertAxis = 0. If not, perhaps I could convert the zero float to a zero integer and use THAT in my if statement? \$\endgroup\$ – Siggytron Feb 23 at 6:33
  • 1
    \$\begingroup\$ Unity lets you configure a deadzone for all axes in project settings. Go to Edit -> Project Settings -> Input, then find the axis named Vertical. Open in with the little arrow, and configure however you want. Also, converting to integer is a fairly bad idea, given that GetAxis returns a number between -1 and 1 :) \$\endgroup\$ – Alex F Feb 23 at 6:47
  • 1
    \$\begingroup\$ It's pretty late for me so I'm going to try this tomorrow. will report back \$\endgroup\$ – Siggytron Feb 23 at 7:07
  • \$\begingroup\$ I tried a couple of things including your suggestions. Unfortunately I still am having the issue where Unity recognizes that it hasn't fulfilled the if condition statement, but still applies the upward force anyway. I can't figure out why. For the time being I am going to try to learn a bit about raycasting and see if I can implement a raycasting solution to this problem. I had wanted to see if I could solve this without raycasting because I had read raycasting adds significant loads to the processing. Plus I don't know how to do raycasting yet. XD \$\endgroup\$ – Siggytron Feb 23 at 16:19
  • 1
    \$\begingroup\$ You shouldn't need to raycast; if all you're trying to do is check when the object hits the ground, you can use Rigidbody.OnCollisionEnter or Rigidbody.SweepTest. \$\endgroup\$ – Alex F Feb 23 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.