- Home /
I can't get it to activate when i press space
I'm new and trying to make a 2D shooter, but i can't get it to work.
Here is the code Don't see its not a 2D rigidbody but down about GetAxis
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
public Rigidbody rb;
public float moveSpeed = 10f;
void FixedUpdate()
{
if (Input.GetAxis("Jump"))
{
rb.AddForce(0, moveSpeed, 0);
}
}
}
Answer by unity_ek98vnTRplGj8Q · Feb 03, 2020 at 07:41 PM
First, check your input settings and make sure that the "Jump" axis actually has the spacebar assigned to it. Also check your rigidbody and make sure isKinematic is not selected.
A couple notes about your code - If you are adding a force then you may want to use GetAxisDown instead of GetAxis, as this will add force every frame that the button is held. Speaking of frames, you don't want to check for input inside of FixedUpdate. You should always check for input inside of Update, and set flags there that can be read in FixedUpdate to apply the forces. This can cause your code not to work depending on the speed of FixedUpdate vs Update
Answer by nAiL1984 · Feb 03, 2020 at 08:31 PM
@sniperhund - You say "when i press space" so you need to use GetButtonDown.
Change Input.GetAxis("Jump") to Input.GetButtonDown("Jump").
For rb.AddForce(0, moveSpeed, 0) add ForceMode. For example rb.AddForce(0, moveSpeed, 0, ForceMode.Impulse).
Answer by Gpopcorn · Feb 03, 2020 at 08:14 PM
Here are some things that might help:
1: Add a new float called "jump" and make the value larger to see if it needs to be increased.
ex.
public float jump = 100f;
2: Instead of:
if (Input.GetAxis("Jump"))
{
}
try this:
if (Input.GetKeyDown("space"))
{
}
Your answer
Follow this Question
Related Questions
Mac standalone doesn't open 0 Answers
Android back button not working 2 Answers
Network functions doesn't work 0 Answers
unity won't start? 3 Answers
Problem with Animator 2 Answers