- Home /
OnCollisionEnter stops working.
I have a platform game there I can go and jump between platforms. I have problem that I can't solve.
I have two platforms booth are exactly the same. Then I go over from one platform to the other my OnCollisionEnter stops to work. But if I jump between the two platforms OnCollisionEnter works as it should. I have checked my code and it's no problem in with it. So I wonder what I have missed with Unity so I can't walk between platforms.
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
//value for movement with keys
public float movspd_jump = 5.0f;
public float movspd_ground = 10.0f;
public float rotate = 10.0f;
private float movspd;
public float jumpspeed = 10.0f;
public bool ready = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// This is the movement for the arrows
float move_Side = movspd * Time.deltaTime * Input.GetAxis("Horizontal");
transform.Translate (Vector3.right * move_Side, Space.World);
float rotate_side = rotate * Time.deltaTime * Input.GetAxis("Horizontal");
transform.Rotate (0, 0, -rotate_side, Space.World);
if(Input.GetKey (KeyCode.UpArrow) && ready ){
rigidbody.AddForce (0,jumpspeed,0);
}
// Ends ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
void OnCollisionEnter(Collision collision) {
movspd = movspd_ground;
ready = true;
}
void OnCollisionExit(Collision collision) {
movspd = movspd_jump;
ready = false;
}
}
I'm not sure why that's happening. It should trigger an enter event when you bridge between the two....
Have you tried messing with OnCollisionStay or debugging the first one with OnCollisionExit to see when/if it's leaving the first collider?
What is your character? Rigidbody or CharacterController? Rigidbodies generate OnCollisionEnter, CharacterControllers don't (except in some rare cases). You should post your script.
$$anonymous$$y character is a Rigidbody. Here is my script: http://pastebin.com/WQHb8Q73 I have debugged to see then it leaves the first collider and it stops work exactly then it touches the other collider.
Answer by aldonaletto · Nov 24, 2012 at 09:49 AM
You should not use Translate with rigidbodies: it confuses the collision system, producing weird behaviours, and may be the cause or your problem. Replace your current Update function with this one:
void Update () {
// get the current rigidbody velocity:
Vector3 rbVel = rigidbody.velocity;
// set the X speed according to the hor arrows:
rbVel.x = movspd * Input.GetAxis("Horizontal");
// if uparrow pressed, set Y speed
if(Input.GetKey (KeyCode.UpArrow) && ready ){
rbVel.y = jumpspeed;
}
rbVel.z = 0; // character doesn't move on Z axis
rigidbody.velocity = rbVel; // set rigidbody velocity
// rotate the object
float rotate_side = rotate * Time.deltaTime * Input.GetAxis("Horizontal");
transform.Rotate (0, 0, -rotate_side, Space.World);
}
Additionally, you should set the Rigidbody/Constraints in the Inspector like this: Freeze Position = Z, Freeze Rotation = XYZ. The rigidbody constraints only affect the physics reactions to collisions - you can rotate the transform as you're currently doing