Unity Annoyance with Physics Help C# Scripting Problem Momentum Issue Please Help
k, so earlier I posted a question and I thought I and this other guy solved it, but the guy tried to help and didn't. He helped me solve one of my problems which was staying on the ground making the input accessible which I thanked him but as of recently I've tried to solve this same problem for 18 hours (2 hours in a span before I took a break then try again) I'm a 15 year old newbie programmer. And my problem is Momentum I want to add momentum so that the MOMENTUM pushes the cube in the air based on the speed it's going. For example: Running and jumping while take you father then walking and jumping cuz of the momentum. My code has been good so far, it's not moving via input in the air which I want. And the input keys are only accessable on the ground which I want. It's been good so far. But to be extremely clear I want the momentum from the cube's movement to translate it so it moves it in the air. So say the cube is moving, if it moves and jumps based on the direction it's going and how fast it's going if you jump the momentum of the cube pushes it.
My whole movement script:
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour
{
public GameObject Cube;
public Transform Cubes = null;
static bool Groundedson = true;
public float jump = 5.0f;
public float jump_Force = 5.0f;
static bool Running = true;
//floats and integers for movement
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;
Quaternion Quad = Quaternion.identity;
//colors
public float upForce = 100.0f; //value of force for up direction
//Start of my updates.
void FixedUpdate()
{
Rigidbody Cube = GetComponent<Rigidbody>();
Vector3 v = GetComponent<Rigidbody>().velocity;
if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow) && Groundedson)
{
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.DownArrow) && !Input.GetKey(KeyCode.UpArrow) && Groundedson)
{
transform.Translate(-Vector3.forward * MoveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow) && Groundedson && !Input.GetKey(KeyCode.RightControl) && !Input.GetKey(KeyCode.LeftControl) && !Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vector3.left * MoveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow) && Groundedson && !Input.GetKey(KeyCode.RightControl) && !Input.GetKey(KeyCode.LeftControl))
{
transform.Translate(Vector3.right * MoveSpeed * Time.deltaTime);
}
GetComponent<Rigidbody>().velocity = v;
if ((Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) && Input.GetKey(KeyCode.RightArrow))
transform.Rotate(Vector3.up * RotationSpeed * Time.deltaTime);
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(-Vector3.up * RotationSpeed * Time.deltaTime);
//Rotation!
else if (Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.
RightArrow) && Groundedson == true)
transform.Rotate(Vector3.up * RotationSpeed * Time.deltaTime);
else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl) && Input.GetKey(KeyCode.LeftArrow) && Groundedson == true)
transform.Rotate(-Vector3.up * RotationSpeed * Time.deltaTime);
// Running
if(!Running)
if (Input.GetKeyDown(KeyCode.Tab) && (Groundedson == true))
{
Running = true;
MoveSpeed = 12;
jump =5;
}
if (Input.GetKeyUp(KeyCode.Tab) && (Running == true) && (Groundedson == true))
{
MoveSpeed = 4;
Running = false; // Running is now false, canceling the speed.
jump = 3;
}
//Change from true to false VVVVVVVVVVVVVVV
// Color functionsyeah
if (Input.GetKeyDown(KeyCode.R) && Groundedson == true)
{
GetComponent<Renderer>().material.color = Color.red;
}
if (Input.GetKeyDown(KeyCode.G) && Groundedson == true)
{
GetComponent<Renderer>().material.color = Color.green;
}
if (Input.GetKeyDown(KeyCode.B) && Groundedson == true)
{
GetComponent<Renderer>().material.color = Color.blue;
}
if (Input.GetKeyDown(KeyCode.Y) && Groundedson == true)
{
GetComponent<Renderer>().material.color = Color.yellow;
}
if (Input.GetKeyDown(KeyCode.C) && Groundedson == true)
{
GetComponent<Renderer>().material.color = Color.cyan;
}
if (Input.GetKeyDown(KeyCode.C) && Input.GetKeyDown(KeyCode.Space) && Groundedson == true)
{
GetComponent<Renderer>().material.color = Color.clear;
print("Cleared");
}
}
//Jumping system
//When it enters the floor it can jump again :D.
void OnCollisionEnter(Collision col)
{
Groundedson = true;
if (col.collider.tag == "Floor")
{
gameObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f);
}
}
//Where my jumping takes place so my gameobject(Cube) can only jump when it's on the ground on the collision with the "floor" tag.
void OnCollisionStay(Collision col)
{
if (col.collider.tag == "Floor")
if (Input.GetKey(KeyCode.Space) && !Input.GetKeyDown(KeyCode.C))
{
GetComponent<Rigidbody>().AddForce(Vector3.up * jump, ForceMode.Impulse);
Vector3 v = GetComponent<Rigidbody>().velocity;
v = friction;
gameObject.transform.position += v;
//adds velocity upward
}
}
//My exit so my input commands become inactive which I like.
void OnCollisionExit(Collision col)
{
if (col.collider.tag == "Floor")
{
gameObject.transform.rotation = new Quaternion(0f, 0f, 0f, 0f);
float a = GetComponent<Rigidbody>().drag;
a += jump;
Groundedson = false;
}
}
//End of the script.
}
The problem is that my cube jumps straight up, (no momentum) but I want real life physics. But I WANT when it jumps say it's going forward I want it to jump up in the air and the MOMENTUM move it forward not the input keys. It's been hard out here for a programmer. Please help lol if you can tell I'm pretty mad, cuz' I've been trying to solve this for almost a day now and I've googled so much stuff and I've tried everything. If someone can help pls, I will give them a virtual cookie with virtual milk please man. End the struggle :(.
You should add to your old question ins$$anonymous$$d of posting a duplicate.
$$anonymous$$omentum is a side effect of the physics engine, when using AddForce.
You people didn't give meh anything, so I finally got it to work :D Props to all of you that help but THIS IS what I wanted
if (col.collider.tag == "Floor")
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
GetComponent<Rigidbody>().AddRelativeForce(Vector3.up +Vector3.forward * jump, Force$$anonymous$$ode.VelocityChange);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
GetComponent<Rigidbody>().AddRelativeForce(Vector3.up + Vector3.right * jump, Force$$anonymous$$ode.VelocityChange);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
GetComponent<Rigidbody>().AddRelativeForce(Vector3.up + Vector3.left * jump, Force$$anonymous$$ode.VelocityChange);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow) && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && !Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
GetComponent<Rigidbody>().AddRelativeForce(Vector3.up + -Vector3.forward * jump, Force$$anonymous$$ode.VelocityChange);
}
I improvised so now it has momentum from movement that can be adjusted. Addforce has no automatic momentum lol. It just went straight up without the extra vector3. Ty for trying to help though. SO HYPE BEEN TRYING FOR DAYS
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D not working 1 Answer
Rigidbody is colliding terrain on isKinematic = false. call although meshes don't touch 0 Answers
Trigger Object is still moving objects? 1 Answer
Collision is occurring visibly, but is not registering in OnCollisionEnter 0 Answers
Move Platforms (Elevator / Lift) 0 Answers