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 PhoenixBlackReal · Jan 27, 2015 at 07:37 PM · spriteoverlap

Drag a sprite around with mouse, but have it not overlap other sprites

Basically, the user can drag sprites around in the scene, yet they cannot overlap. I sort of managed to achieve this using colliders, however, I experience collider tunneling. It's a real headache - how to have the sprites (irregular shape, no primitives available) move around, but not overlap other sprites.

Edit: Alright, asking questions after 18+ hours of frustrating work is not a good idea! To elaborate more on the question - all of the sprites have got convex mesh colliders. I'm using rigidbody.movePosition to drag the sprite. This works really well if I move the mouse slowly, but whenever I start moving fast - the sprite, with its' collider, starts jumping right through other sprites and so forth. My first idea was to move the sprite with forces, setting a maximum speed, but this didn't quite work with my drag, because it would neither move fast enough, nor position properly when I let go.

Also, while this may seem strange - I'm working in 3D space, rather than 2D, because of the nature of the game and my own camera script, which is my pride and joy, but only works with a perspective camera.

In addition, I'm adding the main script I use for moving around.

 using UnityEngine;
 using System.Collections;
  
 [RequireComponent(typeof(Rigidbody))]
 public class Dragable : MonoBehaviour
 {
  
     public int normalCollisionCount = 0;
     public float collisionMoveFactor = .01f;
     public bool freezeRotationOnDrag = true;
     public Camera cam  ;
     private Rigidbody myRigidbody ;
     private Transform myTransform  ;
     private bool canMove = false;
     private bool gravitySetting ;
     private bool freezeRotationSetting ;
     private int collisionCount = 0;
     private Transform camTransform;
     
     void Start () 
     {
         myRigidbody = rigidbody;
         myTransform = transform;
         cam = Camera.main;
     }
  
     void OnMouseDown () 
     {
         canMove = true;
         gravitySetting = myRigidbody.useGravity;
         freezeRotationSetting = myRigidbody.freezeRotation;
         myRigidbody.useGravity = false;
         myRigidbody.freezeRotation = freezeRotationOnDrag;
     }
  
     void OnMouseUp () 
     {
         canMove = false;
         myRigidbody.useGravity = gravitySetting;
         myRigidbody.freezeRotation = freezeRotationSetting;
         if (!myRigidbody.useGravity) 
         {
             Vector3 pos = myTransform.position;
             myTransform.position = pos;
         }
     }
  
     void OnCollisionEnter () 
     {
         collisionCount++;
     }
  
     void OnCollisionExit () 
     {
         collisionCount--;
     }
  
     void FixedUpdate () 
     {
         if (!canMove)
         {
             return;
         }
  
         myRigidbody.velocity = Vector3.zero;
         myRigidbody.angularVelocity = Vector3.zero;
         
         Vector3 pos = myTransform.position;
         myTransform.position = pos;
  
         Vector3 mousePos = Input.mousePosition;
         Vector3 move = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 9.35f)) - myTransform.position;
         if (collisionCount > normalCollisionCount)        
         {
             move = move.normalized*collisionMoveFactor;
         }
         
             myRigidbody.MovePosition(myRigidbody.position + move);
     }
 }

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 smoggach · Jan 27, 2015 at 09:58 PM 0
Share

You're on the right track. I would use a rigidbody driver. Ins$$anonymous$$d of simply setting the position of your dragged sprite, set the position of something else that it can follow. I would connect my sprite to a distance joint then just move the distance joint around. That way physics will still move your sprite & react to collisions.

avatar image PhoenixBlackReal · Jan 28, 2015 at 10:13 AM 0
Share

I'll try it out, although it's a real mental breakdown for me. I should also elaborate more on the question itself :) And please, elaborate more as well ^w^

0 Replies

· Add your reply
  • Sort: 

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

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 do I check if/where in a sprite another sprite is? 0 Answers

Checking for overlaping sprites in an array 1 Answer

Displaying part of sprite that overlaps with other gameObject 0 Answers

Sprites that moves overlapping 1 Answer

Unity2D sprites overlapping 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