# Unity physics Help Implementing Real Life Physics in Jumping System C#

What up people, i’m a 14 year old turning 15 this year just wanting to see if there’s any way to do this thing I wanna do. So I’m learning some basics about C# code, and I tried making a jumping system, using C# scripting. Collision features have been working wonders It’s been going amazing so far, I’ve solved a lot of my own problems and am moving along.

However, I want to implement real life
jumping, you know how you can’t change
your direction in mid air but when you
run and jump your left over friction
moves you. Anyway, the problem I’m
having is that my cube gameobject can move while
still rising or going down. Want to
make it so my left over friction from
moving will impact movement in the air &
Moving System is disabled in the air.

``````Script that moves the Cube:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{

public GameObject Cube;

//floats here:

public float MoveSpeed = 5.0f;
public float RotationSpeed = 5.0f;
Vector3 Vick = Vector3.forward + Vector3.right;
Vector3 Vicky = Vector3.right + -Vector3.forward;
Vector3 Vickyy = -Vector3.forward + Vector3.left;
Vector3 Vickyyy = Vector3.left + Vector3.forward;
Vector3 Rod = Vector3.down + Vector3.right;
Vector3 Rods = Vector3.up;

// Use this for initialization

void FixedUpdate()
{

Rigidbody Cube = GetComponent<Rigidbody>();

//Translation aka Moving right left up down whatever
if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}

else if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.UpArrow))
{

transform.Translate(-Vector3.forward * MoveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Vector3.left * MoveSpeed * Time.deltaTime);

}

else if (Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector3.right * MoveSpeed * Time.deltaTime);
}

//Diagonal movement lol
if (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vick * MoveSpeed * Time.deltaTime);
}

else if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vicky * MoveSpeed * Time.deltaTime);
}

if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(Vickyy * MoveSpeed * Time.deltaTime);
}

else if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vickyyy * MoveSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Rod * MoveSpeed * Time.deltaTime);
}

//Rotation!
else if (Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.
RightArrow))
transform.Rotate(Vector3.up * RotationSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl) && Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(-Vector3.up * RotationSpeed * Time.deltaTime);

// Color functionsyeah

if (Input.GetKeyDown(KeyCode.R))
{
GetComponent<Renderer>().material.color = Color.red;
}
if (Input.GetKeyDown(KeyCode.G))
{
GetComponent<Renderer>().material.color = Color.green;
}
if (Input.GetKeyDown(KeyCode.B))
{
GetComponent<Renderer>().material.color = Color.blue;
}
if (Input.GetKeyDown(KeyCode.Y))
{
GetComponent<Renderer>().material.color = Color.yellow;
}

if (Input.GetKeyDown(KeyCode.C))

{
GetComponent<Renderer>().material.color = Color.cyan;
}
if (Input.GetKeyDown(KeyCode.C) && Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Renderer>().material.color = Color.clear;
print("Cleared");

}

GetComponent<Rigidbody>();

}

}

Script that makes it jump, (two separate scripts)

using UnityEngine;
using System.Collections;

public class cor : MonoBehaviour
{
public float jump = 5.0f;
public GameObject Amy;
Vector3 Rods = Vector3.up;

void OnCollisionEnter(Collision col)
{
if (col.collider.tag == "Floor")
{
gameObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f);

}
}

void OnCollisionStay(Collision col)
{

Rigidbody Amy = GetComponent<Rigidbody>();
if (col.collider.tag == "Floor")
if (Input.GetKey(KeyCode.Space) && !Input.GetKeyDown(KeyCode.C))
{
print("finaLLY");
}

}
void OnCollisionExit(Collision col)
{
if (col.collider.tag == "Floor")
{
gameObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f);

}
}

}
``````

Thank you I know this was hard to solve for me, but for you genius people at unity it’s prob easy :D.
Thanks so much.
-Joshua.

create a boolean called grounded

``````void OnCollisionExit(Collision col)
{
if (col.collider.tag == "Floor")
{
gameObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f);
grounded = false;
}
}
``````

.

``````void OnCollisionEnter(Collision col)
{
if (col.collider.tag == "Floor")
{
gameObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f);
grounded = true;
}
}
``````

then check for grounded when moving left and right

``````else if (Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow) && grounded)
``````

.

``````else if (Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow) && grounded)
``````

That way you will only be able to control left and right movement while colliding with the ground

@konomira,
You helped me sir, but it’s not exactly (trying not to sound mean) what I wanted. I appreciate you helping me, but I’ve been looking at tutorials for hours on this. But there would be a way to pick up speed and then use that speed as a variable to affect your jumping. Similar too super Mario 64, where you can rotate but not move. But your running speed affects how far you go? You can’t move in the air but you can rotate. to affect the direction before you jump.
Your amazing script (no sarcasm) helped but I can only jump straight up,
for example: walk somewhere and jump then run and jump, you can see you went father because of the friction you jumped while running and walking and jumping you go slower.