Reputation: 1
I don't know why the Insantiation doesn't work. I'm trying to do it with the space bar. Or could it be something about the axis?
public GameObject projectile;
public Transform padre, hijo;
public Rigidbody r;
private float value;
void Start () {
padre = transform.parent;
hijo = transform.GetChild (0);
value = 0;
}
void Update () {
float j = Input.GetAxis ("Jump");
if (j == 1 && value == 0) {
Instantiate (projectile, hijo.position, hijo.transform.rotation);
}
float h = Input.GetAxis ("Horizontal2");
r.transform.Translate (h * Time.deltaTime, 0, 0);
float v = Input.GetAxis ("Vertical2");
padre.Rotate (0, 0, -v * Time.deltaTime * 20);
value = j;
}
Upvotes: 0
Views: 38
Reputation: 125435
Comparing float is never a good idea.
The j
variable must equal 1
and value
variable must be 0
before that if statement becomes true. Input.GetAxis
returns values between 0
and 1
.
If all you want to do is instantiate a GameObject when the Space key is pressed they simply use Input.GetKeyDown
instead of Input.GetAxis
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(projectile, hijo.position, hijo.transform.rotation);
}
Upvotes: 2
Reputation: 2319
You're checking j to exactly equal 1. However, getAxis returns a float that will be close, but not exactly equal to 1. Instead check for when j is close enough to 1.
For instance (pseudocode):
float j = Input.GetAxis ("Jump");
float ep=.1f;
if (Math.abs(j-1)<ep && value == 0) {
//Instantiate
Also, you should make sure that value
is in fact equal to 0 (You might have some other code modifying it beyond whats shown)
Upvotes: 0