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:
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;
}
}
}