• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by SpaceFace15 · Oct 14, 2015 at 09:38 AM · c#collisionphysicsrigidbodyunity5

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 :(.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Oct 13, 2015 at 04:50 PM 0
Share

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.

avatar image SpaceFace15 meat5000 ♦ · Oct 14, 2015 at 12:58 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

40 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I prevent stacked objects from being spoiled by collisions? 2 Answers

How to Change Rigidbody Type OnCollision/Trigger with Script? 1 Answer

Ray cast to object to transform, instead all objects with script transform. 0 Answers

My Character is Not Moving 0 Answers

Rigid Body and Collision 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges