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 TheQuantumGamer · Jan 31, 2015 at 07:22 PM · onmousedowndraggingonmousedrag

Clicking and dragging a gameObject

I am currently dragging a gameobject like this.

 Vector3 screenSpace;
 Vector3 offset;
 void OnMouseDown()
 {
     
     //translate the cubes position from the world to Screen Point
     screenSpace = Camera.main.WorldToScreenPoint(transform.position);
     //calculate any difference between the cubes world position and the mouses Screen position converted to a world point  
     offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
 }

 private float speed = 7.5f;



 void OnMouseDrag()
 {
         Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); 
         Vector3 curPosition =  Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
         transform.position = curPosition;
     }

     
 }

Sometimes a user doesn't click directly on an object so is there a way that if a user is between 3-7 pixels off, it still counts as a click. Any help is appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by VesuvianPrime · Jan 31, 2015 at 08:00 PM

Is it an adequate solution to increase the size of your colliders?

Perhaps you could add a child GameObject with a collider on a different layer?

Comment
Add comment · Show 3 · 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 TheQuantumGamer · Jan 31, 2015 at 08:09 PM 0
Share

I cannot increase the size of my colliders as I am using them for physics. But, how would I go about detecting touch on a child gameobject that is on another layer?

avatar image VesuvianPrime · Jan 31, 2015 at 10:07 PM 0
Share

Simply create an empty GameObject, attach a Collider component, set the layer of the GameObject and attach something like this:

 public class $$anonymous$$ouseCollider : $$anonymous$$onoBehaviour
 {
     public $$anonymous$$yClickableScript target;

     void On$$anonymous$$ouseDown()
     {
         if (target != null)
             target.$$anonymous$$yClick$$anonymous$$ethod();
     }
 }

This is extremely rough, but I think you get the idea.

avatar image TheQuantumGamer · Feb 01, 2015 at 01:07 PM 0
Share

Ah ok, thanks for the advice. I'm going to try it now.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make different actions using OnMouseDrag with two different colliders on one Game Object. 0 Answers

Rotate a spawned object whith onmousedrag and move it with onmousedown 0 Answers

UI element blocking OnMouseDrag()? 1 Answer

Dragging object with Mouse causes it to fly away 0 Answers

Having player object always snap to mouse position 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