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;
Quaternion Quad = Quaternion.identity;
// Use this for initialization
//Start of my updates.
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))
{
Amy.AddForce(Rods * jump, ForceMode.Acceleration);
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.
Answer by Konomira · Oct 11, 2015 at 06:21 PM
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
Wow a bool, nice man XD seems so simple but ty I was stumped for a while. You really saved meh, thanks bro.
Answer by SpaceFace15 · Oct 11, 2015 at 08:06 PM
@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.
Your diagonal movement statements were unnecessary, I've neatened up your code.
Vector3 v = GetComponent<Rigidbody>().velocity;
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow) && grounded)
{
v.z = $$anonymous$$oveSpeed * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)&& grounded)
{
v.z = -$$anonymous$$oveSpeed * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow) && grounded && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightControl) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftControl) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
v.x = -$$anonymous$$oveSpeed * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)&& grounded && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightControl) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftControl))
{
v.x = $$anonymous$$oveSpeed * Time.deltaTime;
}
GetComponent<Rigidbody>().velocity = v;
if ((Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightControl) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftControl)) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
transform.Rotate(Vector3.up * RotationSpeed * Time.deltaTime);
if ((Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftControl) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightControl)) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
transform.Rotate(-Vector3.up * RotationSpeed * Time.deltaTime);
.
void OnCollisionStay(Collision col)
{
if (col.collider.tag == "Floor")
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * jump, Force$$anonymous$$ode.Impulse);
print("finaLLY");
}
}
Rigidbody Amy and Vector3 Rods were both unnecessary. I also switched the Force$$anonymous$$ode from Acceleration to Impulse as I find it gives a better impression of a jump.
Your answer
Follow this Question
Related Questions
how do i collide and kill the enemy while pressing space Unity 5 C# 2 Answers
Skip OnTriggerEnter isn't working at all 1 Answer
Is the Input System not working properly? (OSX Unity Editor 5.4.1) 2 Answers
Properly attach to a GameObject after collision? 0 Answers
Stuck inside of Composite Collider when using Platform Effector 2D 2 Answers