Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FreakinOutMan · Mar 26, 2013 at 08:58 PM · collisioncollision detectionmouse follow

Detect Collisions with Object following Mouse Movement

Hi Everyone,

Have a script that makes a block follow by mouse movement which is working just fine. However, this allows the held object to move directly through other objects. Apparently using either heldBlock.rigidbody.MovePosition() or setting heldBlock.transform.position overrides any sort of collision detection?

So I'm wondering... is there a way to have both of these things at once? Following the mouse while still performing collisions (which would then halt the object's movement)? Or is there a different way to manipulate my object to make this work?

I've been looking all over the place, but I couldn't find this specific question... sorry if it's a re-post.

Thanks in advance, and have a super fantastic day! -Matt

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

2 Replies

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

Answer by FireHaze · Mar 26, 2013 at 10:18 PM

Hello there, try to set an empty gameobject with an spring joint, set the rigidbody it adds automatically to your empty gameobject to be kinematic, after that you will set your block's rigidbody as target in the spring joint properties, and finally you add and mod your current block's script to handle the empty gameobject instead of directly move the block, as a result the block will follow by forces your empty gameobject colliding against stuff in the way, you just have to play a bit with the spring, damp forces and drag to set it properly. Give it a try :)

Comment
Add comment · Show 4 · 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 FreakinOutMan · Mar 27, 2013 at 12:32 AM 0
Share

Thanks for the reply! This sounds like a great idea, let me give it a shot and I'll get back to you! =D

avatar image FreakinOutMan · Mar 27, 2013 at 01:30 AM 0
Share

Is there a simple way to make the object actually snap to the mouse location? I don't want it bouncing about, but I've made the values as high/low as I could.... for example, I set max distance to zero, damper to 1e-5, and Spring to 10000.

Should I try hinge joint ins$$anonymous$$d? It's a rather odd scenario because I want to control the movement of the object entirely with the mouse, yet I still want some collision detection to occur (which ideally would then prevent the object from following the mouse).

avatar image FireHaze · Mar 27, 2013 at 01:44 AM 0
Share

umm, you should try to mess around with another property, I didn´t specify in my first answer what I meant with "drag", but you should try to increase the drag value for your "controlled" rigidbody (not the one that has the spring), in this case that one should be the block's rigidbody, try with 100, 1000, etc... It will avoid your object from being bouncing and moving like a moon around a planet lol.

The only problem I found with this method that I am still trying to solve is to control the rotation of the controlled gameobject, you can always freeze it or set a high angular drag to avoid it from spinning, but if you freeze it you won´t be able to handle rotation by code. So if you find a better alternative to handle an object using collisions and physics (in my case a tennis racket), please let me know :) for now try that, I think it will work just fine.

avatar image FreakinOutMan · Mar 28, 2013 at 08:05 PM 0
Share

The drag property is exactly what I needed! I'm still working with it, trying to get collisions to work exactly as I intend, but I think I'll get it soon with your suggestion! As far as rotation goes, I have it easy because I don't want my pieces to rotate, so I just froze the rotation values. I think you can do that and still change the values in code, like this: yourObj.transform.Rotate(new Vector3(0.0f,90.0f,0.0f));

avatar image
0

Answer by cello_91 · May 08 at 11:30 AM

This is a super old thread, but maybe someone wants to do the same..

I found a better way than the spring joint mentioned here.

This thread

https://forum.unity.com/threads/collision-detection-while-dragging-object.182493/

links to this video

https://www.youtube.com/watch?v=ZfjVR-0ZFHU

which explains a solution.

If you don't want to watch or read too much:

Basically you should stop setting the transform, because this cancels physics!

This is all you need:

 private void Update()
 {
     rb.velocity = (getWorldPosition() - transform.position) * followSpeed;
 }

 private Vector3 getWorldPosition()
 {
     lastMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     lastMousePos.z = 0;
     return lastMousePos - offset;
 }
 
 private void OnMouseDown()
 {
     lastMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     lastMousePos.z = 0;
     offset = lastMousePos - transform.localPosition;
 }
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

12 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

Related Questions

OnCollisionEnter not working with CharacterController 1 Answer

Rigidbody MoveTo considering collisions 1 Answer

Smooth movement for the block upon player collision,Smooth box movement 0 Answers

How does Unity detect collision? 0 Answers

Collision detection issue 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