Hi! I create a moving script and “jumping”. But when I holding up arrow character fly no simple once jump.
My script:
using UnityEngine;
using System.Collections;
public class Moving : MonoBehaviour {
public float speed = 10f;
public Vector2 maxVelocity = new Vector2(3, 5);
public float JumpPower = 0.25f;
private Rigidbody2D RB;
void Start () {
RB = GetComponent();
}
void Update () {
var forceX = 0f;
var forceY = 0f;
var absVelX = Mathf.Abs(GetComponent().velocity.x);
if (Input.GetKey(“right”))
{
if (absVelX < maxVelocity.x)
forceX = speed;
transform.localScale = new Vector3(1, 1, 1);
}
else if (Input.GetKey(“left”))
{
if (absVelX < maxVelocity.x)
forceX = -speed;
transform.localScale = new Vector3(-1, 1, 1);
}
GetComponent().AddForce(new Vector2(forceX, forceY));
{
if (Input.GetKey(“up”))
{
RB.AddForce(new Vector2(0, JumpPower), ForceMode2D.Impulse);
}
}
}
}