• 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
Question by mohammad1369 · Mar 21, 2021 at 08:59 AM · collisiononcollisionenteronmousedown

why onCollisionEnter2D doesn't work when i use OnMouseDown or Up?

hi in android I make a game, and use OnMouseDown & UP for drag my object(player). there are some enemy that when player collide them it die. but when i drag player(on mouse down) to enemy collider, Oncollision2D doesn't work. please help me how to solve it. thanks.

Comment

People who like this

0 Show 7
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 toficofi · Mar 21, 2021 at 01:17 PM 1
Share

Does the collision work when you are not dragging the player?

Also, I think it would be helpful if we can see the code that controls dragging the player.

avatar image mohammad1369 toficofi · Mar 22, 2021 at 02:17 PM 0
Share

@toficofi yes, when player collide enemy without dragging it work properly. i also added a script with oncollisionenter function to enemy but when i drag player it doesn't work. when player hit enemy itself both script work properly. thanks.

avatar image toficofi mohammad1369 · Mar 22, 2021 at 08:34 PM 0
Share

It would be good if you could share the code you use for dragging the player.

Show more comments
avatar image $$anonymous$$ · Mar 23, 2021 at 12:56 AM 0
Share

Are the player and enemy on the same layer?

avatar image mohammad1369 $$anonymous$$ · Mar 23, 2021 at 01:36 PM 0
Share

@Hidden_walker23 yes, both in default layer.

avatar image logicandchaos · Mar 23, 2021 at 02:05 PM 0
Share

It's probably based on how you are moving your player when they mouse is down, you you are just setting it's position to the mouse position then physics won't work, you would have to make it move towards the mouse cursor using physics to not break the collision. I think. Posting your code would help a lot.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by ahsen35813 · Mar 23, 2021 at 04:20 PM

I am guessing that this is because you are copying the mouse transform when moving it. Collisions will only be registered if at least one of the objects has a non kinematic rigidbody that is active. Try using something like playerRigidbody.AddForce(player.transform.position - mouse.transform.position) to pull the rigidbody player towards the mouse instead of just copying the transform. Another thing that might work is using OnCollisionStay() instead, because that should trigger as long as there is an overlap and the player's rigidbody is active.

Comment

People who like this

0 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 mohammad1369 · Mar 23, 2021 at 06:56 PM 0
Share

thank you. i changed player's rigid body as kinematic and enemy's RB was kinematic so oncollision didn't work. i change enemy RB as dynamic then fix it.

Collisions will only be registered if at least one of the objects has a non kinematic rigidbody that is active.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

179 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

method/function to check collision between objects 0 Answers

Weird Collision Detection with a Cube and OnCollisionEnter 2 Answers

Collision Query 1 Answer

OnCollisionEnter doesnt work when instantiated 1 Answer

CharacterController and Normal rigidbody Box Collider Collision issues 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