Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 10, 2015 at 09:39 PM · c#unity 5collisioninputcharactercontroller

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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
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 SpaceFace15 · Oct 11, 2015 at 07:14 PM 1
Share

Wow a bool, nice man XD seems so simple but ty I was stumped for a while. You really saved meh, thanks bro.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
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 Konomira · Oct 11, 2015 at 08:46 PM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

38 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

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


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