Hi, I’m trying to make movement where the character can go freely left and right but can only jump so far upwards (and not infinitely, like GetAxisRaw would allow). I’m having an issue with line 25 wherein jumping is “bool” and not “float”, and I’m not sure how to correct this issue to still allow only one jump press at a time and avoid infinite jumping. I’m pretty new to coding so sorry if this is something obvious.
using UnityEngine;
using System.Collections;
public class VelocityMove : MonoBehaviour {
// UPDATED: Now Vector2 variables
public Vector2 velocity = new Vector2(15f,15f); // Used to store velocity
private Vector2 direction; // Used to store input directions (-1, 0, or 1)
public Rigidbody2D rb2D; // This gameObject's Rigidbody2D component
// Use this for initialization
void Start ()
{
// Store reference to the Rigidbody 2D attached to this gameObject
rb2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
// More responsive input in Update()
// Use Input Manager for input (-1 for left, 1 for right, 0 for no movement)
direction.x = Input.GetAxisRaw ("Horizontal");
direction.y = Input.GetKeyDown (KeyCode.W);
}
// Update used for physics
void FixedUpdate()
{
rb2D.velocity = Vector2.Scale(velocity, direction);
}
}