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 gdv · Apr 11, 2014 at 03:59 PM · bugdragthrough

Click and drag - Object go through other objects when dragging

Hello,

I created a script with which you can drag objects, but when I drag an object it doesn't collide with other objects and go through them... Here's the code:

 var speed:float;
 var target : Vector3;
 var start : Vector3;
 private var pos;
 var self : GameObject;
 var distanceToObject : float = 7.5;
 var dragging : boolean = false;
  
 function Start()
 {
     start = transform.position;
     pos = transform.position;
 }
  
 function Update ()
 {
     if(Input.GetMouseButtonDown(0))
     {
     var ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0.0));
     var hit : RaycastHit;
     if(Physics.Raycast(ray, hit, distanceToObject))
     {
     if(hit.collider.gameObject == self)
     {
        Destroy(self.GetComponent(Rigidbody));
        dragging = true;
     }
     }
     }
     if(Input.GetMouseButtonUp(0)) {
         if(dragging) {
         self.AddComponent(Rigidbody);
         dragging = false;
         }
     }
     if(dragging) {
         pos = Input.mousePosition;
         pos.z = 3.5;
         pos = Camera.main.ScreenToWorldPoint(pos);
         transform.position = Vector3.Lerp(transform.position, pos, speed*Time.deltaTime);
     }
 }

What should I do?

Also you need to add this if you want it to work right:

 function Start() {
 Screen.lockCursor = true;
 }
Comment
Add comment · Show 4
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 robertbu · Apr 11, 2014 at 04:12 PM 0
Share

Exa$$anonymous$$e the standard DragRigidbody.js script. If you don't have it you can get it by:

 Assets > Import Package > Scripts
avatar image gdv · Apr 11, 2014 at 06:45 PM 0
Share

Okay! Thanks =)

avatar image robertbu · Apr 11, 2014 at 06:56 PM 0
Share

After you do the Import Package, type DragRigidbody in the search field at the top of the Project window...or you can just create a game object and then click on the Add Component button and add the DragRigidbody script to a game object. You will want to copy the code to a new file before you start modifying so that you have the original file with the original name available.

avatar image gdv · Apr 11, 2014 at 08:19 PM 0
Share

Did it! Thanks!

1 Reply

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

Answer by Anxo · Apr 11, 2014 at 06:49 PM

When ever you set transform.position, you are telling it it should be there in absolute terms, you are not telling it to move in a direction or anything like that, you are telling it where its position should be so it will not care about collisions.

If you have a rigidbody and collider on the object, you can use rigidbody.velocity to move it to your mouse but it will not go through object.

Comment
Add comment · 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 gdv · Apr 11, 2014 at 08:23 PM 0
Share

Thank you very much, but i am just going to put the Drag Rigidbody script =)

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

22 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

Related Questions

Scene and hierarchy suddenly empty 0 Answers

Unity 2D Character controller bug problem 0 Answers

Unity Backdating itself 0 Answers

Normal map color change problem. BUG? 1 Answer

A node in a childnode? 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