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
1
Question by Retruate · Aug 25, 2015 at 09:15 PM · c#unity 5oncollisionenter

OnColliderEnter stopping after a few seconds!

Sorry for the newbie question, but there is something I really can't figure out myself.

I'm trying to make a scene with a cube that can jump and move when you press the corresponding arrow keys.

I'm using OnCollisionStay and an if statement to only let the cube jump when colliding with the plane below it, so it doesn't jump an insane amount of time, or jump in midair. When I run the scene, the cube falls on to the plane, and the OnCollisionEnter function runs, but after a few seconds, it stops running and you can't jump. I don't know what I'm doing wrong, can someone help?

Here's my code.

 using UnityEngine;
 using System.Collections;
 
 public class MoveCube : MonoBehaviour {
 
     //Variables
     public float movementSpeed = 0.3f;
     Rigidbody c_rigidbody;
 
     void Start() {
         c_rigidbody = GetComponent<Rigidbody>();
         StartCoroutine (Movement ());
     }
 
     IEnumerator Movement(){
 
         while (3 == 3) {
 
             //Sets variables to key codes.
             bool forward = Input.GetKey (KeyCode.UpArrow);
             bool backward = Input.GetKey (KeyCode.DownArrow);
             bool left = Input.GetKey (KeyCode.LeftArrow);
             bool right = Input.GetKey (KeyCode.RightArrow);
 
             //Asks if keys are being pressed. If "yes", moves object's transform.
             if(forward){
                 transform.Translate (Vector3.forward * movementSpeed);
             }
             if(backward){
                 transform.Translate (Vector3.back * movementSpeed);
             }
             if(left){
                 transform.Translate (Vector3.left * movementSpeed);
             }
             if(right){
                 transform.Translate (Vector3.right * movementSpeed);
             }
 
             yield return null;
                         }
 
     }
     //If collision: tells console "Colliding." & Adds upward force to provide a "jump".
     void OnCollisionStay(Collision collision){
         Debug.Log ("Colliding.");
         if (Input.GetKey (KeyCode.Space)) {
             c_rigidbody.AddForce (Vector3.up * 500.0f); 
         }
     }
 }

Thanks so much for your help.

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

1 Reply

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

Answer by Positive7 · Aug 25, 2015 at 09:30 PM

Input should be called in Update() or Corutine see Documentation : http://docs.unity3d.com/ScriptReference/Input.html

Using it in FixedUpdate or OnCollision... OnTrigger... is not a good idea. In Tag manager add a new Tag let's call it " Terrain " (without quotes) or whatever name you want. Then use this code :

 using UnityEngine;
 using System.Collections;
 
 public class test : MonoBehaviour {
     
     //Variables
     public float movementSpeed = 0.3f;
     Rigidbody c_rigidbody;
     bool canJump;
     
     void Start() {
         c_rigidbody = GetComponent<Rigidbody>();
         StartCoroutine (Movement ());
     }
     
     IEnumerator Movement(){
         
         while (3 == 3) {
             
             //Sets variables to key codes.
             bool forward = Input.GetKey (KeyCode.UpArrow);
             bool backward = Input.GetKey (KeyCode.DownArrow);
             bool left = Input.GetKey (KeyCode.LeftArrow);
             bool right = Input.GetKey (KeyCode.RightArrow);
             
             //Asks if keys are being pressed. If "yes", moves object's transform.
             if(forward){
                 transform.Translate (Vector3.forward * movementSpeed);
             }
             if(backward){
                 transform.Translate (Vector3.back * movementSpeed);
             }
             if(left){
                 transform.Translate (Vector3.left * movementSpeed);
             }
             if(right){
                 transform.Translate (Vector3.right * movementSpeed);
             }
             
             yield return null;
         }
         
     }
     void Update(){
         if (Input.GetKey (KeyCode.Space) && canJump) {
             c_rigidbody.AddForce (Vector3.up * 50.0f); 
         }
     }
     void OnCollisionEnter(Collision collision){
         if (collision.gameObject.CompareTag ("Terrain"))
             canJump = true;
     }
     void OnCollisionExit(Collision collision){
         if (collision.gameObject.CompareTag ("Terrain"))
             canJump = false;
     }
 }


Comment
Add comment · Show 2 · 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 Retruate · Aug 25, 2015 at 10:25 PM 0
Share

Thank you very much for your help. I looked at your code, and then edited $$anonymous$$e to do the same thing as yours. It ended up working and now I can continue! :D

avatar image Positive7 · Aug 25, 2015 at 10:45 PM 0
Share

I'm glad! $$anonymous$$eep up the good work!

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

28 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

Related Questions

Unity2d trying to make an enemy react to an attack with a flash. Why isn't this code working? 1 Answer

How to use the results of a dice roll? 2 Answers

How do i Instantiate sub-Points with in Multiple Points?? 0 Answers

OnCollisonEnter2D Not Firing after checking collider 1 Answer

Saved data gets overwritten 0 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