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.
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;
}
}
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
Your answer
Follow this Question
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