Movement.cs

The following is very short script for Unity, which has the correct speed and jump height for use as a foundation.

Movement.cs

using UnityEngine;
using System.Collections;

public class MovmentPrototype : MonoBehaviour {

public int speed = 5;
public int jumpHeight = 7;
private Rigidbody2D myRB2D;
private bool inAir = false;

// Use this for initialization
void Start () {

// collecting the RigidBody2D from the object the script is attached to.
// This allows the script to be added to any object that needs it, without editing
myRB2D = this.GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update () {

RunWIthPhsycis();

if (Input.GetKeyDown("space") && (inAir == false))
{

myRB2D.velocity = new Vector2(myRB2D.velocity.x, (jumpHeight + myRB2D.velocity.y));
inAir = true;

}

Debug.Log(inAir);

}

void RunWIthPhsycis()
{

myRB2D.velocity = new Vector2((Input.GetAxis("Horizontal") * speed), myRB2D.velocity.y);

}

void OnCollisionEnter2D(Collision2D collision)
{

if (collision.gameObject.tag.Equals("Floor"))
{

inAir = false;

}

}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s