Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by j0ffe · Nov 23, 2012 at 08:00 PM · collisionplatform

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;
     }
 }
Comment
Add comment · Show 3
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 darthbator · Nov 24, 2012 at 12:22 AM 0
Share

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?

avatar image aldonaletto · Nov 24, 2012 at 12:37 AM 0
Share

What is your character? Rigidbody or CharacterController? Rigidbodies generate OnCollisionEnter, CharacterControllers don't (except in some rare cases). You should post your script.

avatar image j0ffe · Nov 24, 2012 at 08:47 AM 0
Share

$$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.

1 Reply

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

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

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 j0ffe · Nov 24, 2012 at 10:44 AM 0
Share

Thanks it solved the problem!

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

12 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

Related Questions

A node in a childnode? 1 Answer

Is this Possible to force gameobjects (coliders etc) to get stretched based on the aspect ratio of the screen? 0 Answers

moving platforms 1 Answer

How do Unity manages collisions in a game internally? 0 Answers

Colliders with JS only 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