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 pognoss · Oct 05, 2012 at 04:24 PM · dragdropcross

Drag and drop with collider

Hi all. My problem is simple. I can drag and drop an item in my scene.But the problem is that my object can't collider with others objects. I don't want my object crosses others. Thank you.

Here my code:

 using UnityEngine;
 using System.Collections;
 
 
 public class fee : MonoBehaviour {
 
     private Vector3 pos;
     public bool vole = true  ;
     private Vector3 screenPos;
     private Vector3 currentScreenPos;
     private Vector3 currentPos;
     private Vector3 lastPosition;
     private Vector3 lastPos;
     private Vector3 offset;
     private Vector3 dir;
     public float moveSpeed;
     private CharacterController controller;
     
     void Start () {
     
     }
     
 
     void Update(){
             MoveUpdate();
     }
     
     void MoveUpdate(){   
         
         
         if(vole){
             if (Application.platform == RuntimePlatform.Android){
             
                 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0));
             }
             else{
             
                 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
                 lastPos = pos;
             }
         }
          
         if (Input.touchCount == 1) {    
 
             Touch f0 = Input.GetTouch(0);
             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
             RaycastHit hit;
 
             if (Physics.Raycast (ray, out hit, 100)) {
                 //On vérifie que l'on clique bien sur la fée     
                 if(hit.collider.tag == "Fee"){
                   if (f0.phase == TouchPhase.Began) {
                         screenPos = Camera.main.WorldToScreenPoint(transform.position);
                         offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(f0.position.x, f0.position.y, 0));   
                     }else if(f0.phase == TouchPhase.Moved){
                             
                         currentScreenPos = new Vector3(f0.position.x, f0.position.y,0);
                         currentPos = Camera.main.ScreenToWorldPoint(currentScreenPos) + offset;
                         transform.position = currentPos;
                         lastPosition = currentPos;
                     }
                 }
             }
             
                 
             
                 
         } 
         
         
 
     }
     
     void OnMouseDrag() {
          transform.position = new Vector3(pos.x,pos.y,0);    
     }
     
     void OnCollisionEnter(Collision collision)
     {
         StartCoroutine("collisionFee");
         
     }
     /*
       void OnTriggerEnter(Collider other) {
       
             StartCoroutine("collisionFee");
         
     }
     */
      IEnumerator collisionFee() {
         vole = false;
         transform.position = lastPosition;
         transform.position = new Vector3(lastPos.x,lastPos.y,0);
         yield return new WaitForSeconds(1f);
         vole = true;
     }
 
 }
 
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 kmeboe · Oct 05, 2012 at 06:17 PM 0
Share

Hmm...if both objects have colliders attached, doesn't Unity handle the collisions for you? Are you trying to do something more than simply keeping the objects from overlapping?

avatar image nikescar · Oct 05, 2012 at 10:40 PM 0
Share

You might look into this: http://docs.unity3d.com/Documentation/$$anonymous$$anual/PlatformDependentCompilation.html to replace the Application.platform == RuntimePlatform.Android method. It's just a preference thing.

2 Replies

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

Answer by nikescar · Oct 05, 2012 at 10:36 PM

This is happening because you are transforming the position of the physics object you are moving. This results in a "teleportation" because you are overriding the physics engine's positioning of the object by assigning the position of the mouse.

If you want to use the physics engine for detection, you have to use physics forces to move the object. Look for the DragRigidBody script under Assets -> Import Package -> Scripts. It should do what you are looking for.

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
avatar image
0

Answer by pognoss · Oct 06, 2012 at 06:30 AM

Seriously thank you Nikescar!! I love you! XD I look for several week. I need help on French forum but no one people helped me. Again thank you!

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

11 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

Related Questions

Drag and drop an external file/folder into a running game window? 2 Answers

Drag item on screen once spawned. 0 Answers

How to move UI image between panels 0 Answers

Drag and Drop Inside Editor 0 Answers

UI eventsystem Raycast Unity 5 does not recognize parent and child 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