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 /
  • Help Room /
avatar image
0
Question by kamilchmiel · Nov 24, 2016 at 09:30 PM · collidertouchdetectionoverlapdragging

Colliders detect collision too late - touch dragging problem

Hi, I'm making mobile game and I have a huuge problem with collision detection. I was looking for answer, but I haven't found any solution.

My game looks almost like "Unlock me". I have few blocks in the scene and player has to move these blocks. But sometimes when I drag block fast, 2 blocks overlap.

To move blocks I use: gameObjectTodrag.transform.position = new Vector3(GOcenter.x, newGOCenter.y, GOcenter.z); and maybe this method of changing block position is wrong.

I would like to stop dragging block exactly in a place of collision, but it detects collision too late. Do you have any idea how can I fix it?

I know there were many similar questions, but I haven't found any solution for my problem. Please help me :) alt text

problem.png (17.3 kB)
Comment
Add comment · Show 5
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 hexagonius · Nov 24, 2016 at 09:47 PM 0
Share

use your collider in conjunction with a rigidbody and move it in FixedUpdate with $$anonymous$$ovePosition.
You're teleporting your object right now, bypassing physics

avatar image kamilchmiel hexagonius · Nov 24, 2016 at 10:14 PM 0
Share

Tried this, but it works the same as changing transform.position in my case.

Any other ideas?

avatar image hexagonius kamilchmiel · Nov 24, 2016 at 10:28 PM 0
Share

is$$anonymous$$inematic is true? at the bottom

https://docs.unity3d.com/ScriptReference/Rigidbody.$$anonymous$$ovePosition.html

Show more comments
avatar image hexagonius · Nov 25, 2016 at 01:03 AM 0
Share

You're saying too late, what are you doing the moment the boxes collide? set them kinematic or something? I think I check with this tomorrow.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Nov 25, 2016 at 09:53 AM

I fiddled a bit with this problem now and must admit I'm confused and disappointed about MovePosition. It effectively only works if the collider you have has a Rigidbody not set to kinematic if you want the other collider be without one. Then you suffer from the velocity that get's applied to it on collision and the fact that the moved one does not stay out of what it collided with... really bad I might say, or at least, what is it even good for? So to deal with the problem velocity is the best way to go and it works like this:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class MoveWithCursor : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler {
 
     Rigidbody rb;
 
     bool dragging;
     Vector3 worldPosition;
 
     void Awake(){
         rb = GetComponent<Rigidbody> ();
     }
 
     void FixedUpdate(){
         if (!dragging)
             return;
         rb.velocity = (worldPosition - transform.position) / Time.deltaTime;
     }
 
     public void OnBeginDrag (PointerEventData eventData)
     {
         dragging = true;
     }
 
     public void OnDrag (PointerEventData eventData)
     {
         worldPosition = Camera.main.ScreenToWorldPoint ((Vector3)eventData.position - Vector3.forward * Camera.main.transform.position.z);
     }
 
     public void OnEndDrag (PointerEventData eventData)
     {
         dragging = false;
     }
 }

You either translate it to whatever movement logic you have, or have a main Camera with PhysicsRaycaster and an EventSystem in a scene where the game happens at world z 0 and attach this to a cube with collider and non Kinematic rigidbody

Comment
Add comment · 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

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

103 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

Related Questions

OnDrag is moving all colliders on Touch? (Not on Standalone) 0 Answers

Cannot call trigger-event when dragging object via touch 1 Answer

Drag two Object by Touch At the same time? 0 Answers

Why my ray doesn't hit the collider? 1 Answer

Conditionals using TouchPhase.Ended 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