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 /
avatar image
0
Question by shenhuang · Oct 16, 2016 at 07:20 AM · collision detectioncollider2dcollisions

OnCollision2D never worked

Here are my code, I was trying to make this game object drop on a large collider.

using UnityEngine; using System.Collections;

public class Player_Behavior : MonoBehaviour {

 public float gravity;
 public float speedX;
 public float speedY;
 public bool freeFall;

 // Use this for initialization
 void Start () {
     freeFall = true;
     gravity = 0.5f;
     speedX = 0;
     speedY = 0;
 }
 
 // Update is called once per frame
 void Update () {
     gravityPull ();
     motionEngine ();
 }

 void OnCollisionEnter2D (Collision2D collision) {
     //if (collision.gameObject.tag == "Platform" && collision.gameObject.transform.position.y < transform.position.y) {
         freeFall = false;
         speedY = 0;
     //}
 }

 private void gravityPull () {
     if (freeFall) {
         speedY -= gravity;
     } else {
         speedY = 0;
     }
 }

 private void motionEngine () {
     transform.Translate (speedX * Time.deltaTime, speedY * Time.deltaTime, 0);
 }

}

Comment
Add comment · Show 6
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 tanoshimi · Oct 16, 2016 at 08:43 AM 0
Share

"Stopped" working? So it used to work? What did you change? Do the objects involved in the collision both have 2D colliders and at least one of them have a non-kinematic Rigidbody2D attached?

avatar image shenhuang tanoshimi · Oct 16, 2016 at 04:07 PM 0
Share

Sorry I rephrased the question. It never worked.

From your advice I tried to add a rigidbody2d onto one of the object with "is kinematic" unchecked, and the function is still not being called.

avatar image conman79 · Oct 16, 2016 at 04:36 PM 0
Share

Using Transform.Translate in conjunction with physics (eg, colliders) can cause issues like this. Try using Rigidbody2D.$$anonymous$$ovePosition ins$$anonymous$$d. You'll need a rigidbody2D component on your dropped gameobject.

Also if you're using Rigidbody2D.$$anonymous$$ovePosition, make sure to call it in FixedUpdate(), not Update();

avatar image shenhuang conman79 · Oct 16, 2016 at 06:18 PM 0
Share

It is still not being called...

using UnityEngine; using System.Collections;

public class Player_Behavior : $$anonymous$$onoBehaviour {

 public float gravity;
 public float speedX;
 public float speedY;
 public bool freeFall;
 public Rigidbody2D rigidBody;

 // Use this for initialization
 void Start () {
     rigidBody = GetComponent<Rigidbody2D> ();
     freeFall = true;
     gravity = 0.5f;
     speedX = 0;
     speedY = 0;
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     gravityPull ();
     motionEngine ();
 }

 void OnCollisionEnter2D (Collision2D collision) {
     //if (collision.gameObject.tag == "Platform" && collision.gameObject.transform.position.y < transform.position.y) {
         freeFall = false;
         speedY = 0;
     //}
 }

 private void gravityPull () {
     if (freeFall) {
         speedY -= gravity;
     } else {
         speedY = 0;
     }
 }

 private void motionEngine () {
     rigidBody.$$anonymous$$ovePosition (new Vector2(rigidBody.position.x + speedX * Time.fixedDeltaTime, rigidBody.position.y + speedY * Time.fixedDeltaTime));
 }

}

avatar image conman79 shenhuang · Oct 16, 2016 at 09:16 PM 0
Share

Hmm strange. Is your floor collider set as a trigger? If so you should be using OnTriggerEnter2D ins$$anonymous$$d.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My OnCollisionEnter says i have 128 Collisions every collision 1 Answer

How to stop a bullet (CircleCollider2D) from continuing to move after hitting a wall? 1 Answer

CircleCollider2D is not registering collisions with some tiles in a TileMapCollider2D 1 Answer

2D collision won't work? 2 Answers

Why there is no Collider.IsTouching(...) ? 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