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
0
Question by saldavonschwartz · Oct 14, 2013 at 02:41 AM · collisionrigidbody

Jerky motion and inconsistent collision detection with planes with rigid bodies

I have a prefab made up of a plane + rigid body + collider, a "selection tracking" script attached to the main camera and global gravity set to 0 in all directions. I'm trying to be able to instantiate prefabs on mouse / touch up and drag existing ones around.

Here's my prefab:

alt text

And this is my "selection tracking" code:

 using UnityEngine;
 
 public class SelectionTracker : MonoBehaviour {
     public GameObject node;
     
     public class UniversalTouch {
         public Vector2 position;
         public TouchPhase phase;
         
         public UniversalTouch(Touch touch) {
             this.position = touch.position;
             this.phase = touch.phase;
         }
         
         public UniversalTouch(Vector2 position, TouchPhase phase) {
             this.position = position;
             this.phase = phase;
         }
     }
     
     UniversalTouch currentTouch;
     GameObject currentlySelectedObject = null;
     
     //    MonoBehaviour:--------------------------------------------------------------------------------------------------
     
     void Start() {
         currentTouch = null as UniversalTouch;
     }
     
     void FixedUpdate() {
         updateTouch();
         if (currentTouch != null) {
             switch (currentTouch.phase) {
                 case TouchPhase.Began: {
                     RaycastHit hit;
                     if (Physics.Raycast(camera.ScreenPointToRay(currentTouch.position), out hit)) {
                         GameObject node = hit.transform.gameObject;
                         if (node != null) {
                             currentlySelectedObject = node;
                             currentlySelectedObject.rigidbody.isKinematic = true;
                         }
                     }
                     else {
                         currentlySelectedObject = null as GameObject;
                     }
 
                     break;    
                 }
                 
                 case TouchPhase.Moved: {
                         if (currentlySelectedObject != null) {
                             currentlySelectedObject.transform.position = camera.ScreenToWorldPoint(new Vector3(currentTouch.position.x, currentTouch.position.y, -camera.transform.position.z));
                         }
                     
                     break;    
                 }
                 
                 case TouchPhase.Stationary: {
                     break;    
                 }
                 
                 case TouchPhase.Ended: {
                 if (currentlySelectedObject == null) {
                     Vector3 newNodeLocation = camera.ScreenToWorldPoint(new Vector3(currentTouch.position.x, currentTouch.position.y, -camera.transform.position.z));
                         GameObject newNode = (GameObject)Instantiate(node);
                         newNode.transform.position = newNodeLocation;
                 }
                 else {
                     currentlySelectedObject.rigidbody.isKinematic = false;
                 }
                     break;    
                 }
             }
         }        
     }
     
     void updateTouch() {        
         if (Input.touchCount == 1) {
              currentTouch = new UniversalTouch(Input.touches[0]);
         }
         
         Vector2 newScreenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         if (Input.GetMouseButton(0)) {            
             if (currentTouch == null) {
                 currentTouch = new UniversalTouch(newScreenPosition, TouchPhase.Began);
             }
             else if (currentTouch.phase == TouchPhase.Began || currentTouch.phase == TouchPhase.Stationary || currentTouch.phase == TouchPhase.Moved) {
                 if (currentTouch.position == newScreenPosition) {
                     currentTouch.phase = TouchPhase.Stationary;
                 }
                 else {
                     currentTouch.phase = TouchPhase.Moved;
                     currentTouch.position = newScreenPosition;
                 }
             }
         }
         else if (currentTouch != null) {
             if (currentTouch.phase == TouchPhase.Stationary || currentTouch.phase == TouchPhase.Moved) {
                 currentTouch.phase = TouchPhase.Ended;
                 currentTouch.position = newScreenPosition;
             }
             else {
                 currentTouch = null as UniversalTouch;
             }
         }    
     }
     
 }

However, I have two problems with this approach:

  1. When I select an existing prefab and drag it around, it doesn't feel smooth but somewhat jerky.

  2. If I drag whichever prefab I select around fast enough, it does intersect other prefabs. That is, for the one I'm dragging to repel off any other prefabs, I have to drag at a somewhat intermediate speed.

I tried solving these issues by:

  • Replacing Update with FixedUpdate

  • playing around with the different interpolation and collision detection modes in the rigid body

  • switching whichever prefab I am dragging to kinematic.

But I still have the same problems. Any thoughts on what am I doing wrong?

screen shot 2013-10-13 at 7.33.51 pm.png (52.8 kB)
Comment
Add comment
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

1 Reply

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

Answer by saldavonschwartz · Oct 15, 2013 at 09:45 AM

  • The jerkiness I solved by doing my position updates in FixedUpdate() and by means of rigidbody.MovePosition()

  • The inconsistent collision detection I solved by following Fattie's answer to a related question of mine: http://answers.unity3d.com/questions/556134/how-could-i-break-apart-or-unstick-two-intersectin.html . The short answer being: make the colliders not too skinny in any dimension.

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

14 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

Related Questions

How do I play the appropriate sound when Bouncing and Rolling? 1 Answer

How to get transform.Translate to work with rigidbodies 2 Answers

Sphere vs Capsule collider collision resolution 0 Answers

Why does the object keep falling? 2 Answers

Changing body type right before the collision response 1 Answer


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