• 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 pab001 · Mar 27, 2014 at 10:54 PM · collisiondragclipping

gameObject goes through wall with onMouseDrag

The issue at hand is that whenever I use OnMouseDrag() function on a GameObject (a ball), too fast, the ball is then dragable through the wall. When I do a slow drag this doesn't happen. The gameObject has a rigidBody attached to it and all of my walls have box colliders attached to it.

I have been wrapping my head on this for the last two weeks and tried different solutions without resolving my issue.

I'm using a raycastHit to get this working and use a transform.position to move the ball to a new vector whenever the ray hits within a certain distance from my walls This works fairly well but dragging needs to be done slow or normal. Fast drag will move the ball through the wall.

Tried using the settings in Time manager in order for the physics engine too.

The only thing that may work is perhaps to include FixedUpdate() within onMouseDrag() function but that results in an error. Not sure how that would work and not familiar with coroutines ans such. Below is my script. Perhaps someone in the community is able to help me out?


 #pragma strict
 
 private var screenPoint:Vector3 ;
 private var offset:Vector3;
 private var speed : float = 10;
 static private var currTrans : Transform = null;
 var cursorTexture : Texture2D;
 var cursorMode : CursorMode = CursorMode.Auto;
 var hotSpot : Vector2 = Vector2.zero;
 
 function  OnMouseOver() { 
  
     screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     currTrans = transform;
     renderer.material.color = Color.green;
 }
 
 function OnMouseEnter () {
     Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
     screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     currTrans = transform;
     renderer.material.color = Color.green;
 }
 
 function OnMouseExit () {
     Cursor.SetCursor(null, Vector2.zero, cursorMode);
       renderer.material.color = Color.white;
       print ("");  
 } 
 
 function OnMouseDrag(){  
 
     if (currTrans != transform) return;{
     }
                                                                                                                                                                                                                                                                                                                    
     var curScreenPoint:Vector3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 9.2f);
     var curPosition:Vector3 = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     transform.position = curPosition;
     
       
     //Debug.Log("Position of ball is " + curPosition.x + ", " + curPosition.z);
     
     var hit : RaycastHit;
         if (Physics.Raycast (transform.position,Vector3(curPosition.x,curPosition.y,curPosition.z), hit)) {
             var distanceObject = hit.distance;
             if (distanceObject < 0.4f){
             gameObject.transform.position=new Vector3(curPosition.x+0.3f,curPosition.y,curPosition.z+0.3f);            
             }
         }
 }
Comment
Add comment · Show 2
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 getyour411 · Mar 27, 2014 at 10:58 PM 0
Share

You really can't reliably move anything with transform.position and expect it to work with colliders/physics. I didn't read the rest since the code isn't formatted, just stopped reading at the use of transform.position

avatar image oxa89 · Sep 15, 2014 at 09:07 AM 0
Share

Any help with this? I am facing a similar problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pab001 · Mar 27, 2014 at 11:37 PM

Hi getyour411. Thanks for your quick response. Tried the rigibody.position instead of transform.position but did not solve the issue. What about FixedUpdate(). I can't seem to get that working with the OnMouseDrag() function. These can't be nested.

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.

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

21 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

Related Questions

How to get movable/pushable cubes right? 0 Answers

Two GameObjects "Pushing" eachother 1 Answer

OnMouseDrag, object getting dragged goes through collider instead of stopping 0 Answers

Collision with drag object 2 Answers

How to stop Kinematic Rigidbody from moving through walls 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