- Home /
Input.GetButton not working
Hello guys. I would ask for your help to solve my problem. I have a cube that walks among an array of gameobjects and this works fine. But the function ' Input. GetButton ' or any other similar command does not work. Below is the code:
public Transform[] pathNodes;
public float movementSpeed = 0.5f;
public bool continuouslyLoop = false;
private float pathPosition = 0f;
private int curNode = 0;
void Update () {
if (pathNodes != null)
{
pathPosition += Time.deltaTime * movementSpeed;
if (pathPosition > 1f)
{
if (pathNodes.Length > curNode+2)
{
pathPosition = 0f;
curNode += 1;
}
else
{
if (continuouslyLoop)
{
pathPosition = 0f;
curNode = 0;
}
}
}
transform.position = Vector3.Lerp( pathNodes[curNode].position, pathNodes[curNode+1].position, pathPosition );
}
if (Input.GetButtonDown("Jump")) {
Jump();
}
}
in the Input$$anonymous$$anager, is "Jump" defined as a button?
Can we get the jump function? And are you sure the "Jump" axis has the appropraite buttons?
rigidbody2D.AddForce(Vector3.up * jumpSpeed * Time.deltaTime);
I have also used and did not work in the console
if (Input.GetButtonDown("Jump")) {
Debug.Log("Test");
}
Answer by sniper43 · Feb 05, 2015 at 04:08 PM
transform.position = Vector3.Lerp( pathNodes[curNode].position, pathNodes[curNode+1].position, pathPosition );
You're lerping the positions while trying to use AddForce to jump. You're lerping every frame, so the velocity doesn't really have time to throw the object into the air.
Remember that Rigidbodies run on a seperate loop and you generally don't want to adjust position when you have a rigidbody or addforce when you have a position. Use LookAt to make your object face then nodes then AddForce(transform.forward, floatForceAmount) or set velocity to propel it forward.
Do this in FixedUpdate()
Otherwise, adapt your node position so that you add a vertical vector to it that represents the current jump height.
Hope this helps.
Your answer
Follow this Question
Related Questions
(C#) A better way to limit actions to once per button press? 2 Answers
GetButtonDown not always firing 1 Answer
Jump and run on a circle (2D) 0 Answers
Divide screen in half for two buttons 1 Answer
3D coyote time 1 Answer