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 siddharth3322 · May 25, 2014 at 08:07 AM · gameobjectspritedragunity4.3

Object dragging at manual point

For my 2d game, I want to drag object in horizontal direction.

alt text

For dragging, I have used following code.

 private bool isTouchDown;
 
     void Update ()
     {
         if (Input.GetMouseButtonDown(0)) {
             Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             RaycastHit2D hit = Physics2D.Raycast (pos, Vector2.zero);
             if (hit != null && hit.collider != null && hit.collider.CompareTag(Constants.TAG_PLAYER_PADDLE)) 
                 isTouchDown=true;
 
         }else if(Input.GetMouseButtonUp(0))
                 isTouchDown=false;
 
         if(isTouchDown){
             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             touchPosition.y = -8f;
             touchPosition.z = -1;
             touchPosition.x  = Mathf.Clamp(touchPosition.x,-3.5f,3.5f);
             transform.position = touchPosition;
         }
     }

By default object dragged from centre. That point I want to change. If player touch object from corner then object gets dragged from that point not centre one.

I know I have to use some math here but at present I can't able to calculate it. Please provide some guidance in this.

yellow_bar.png (3.4 kB)
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
0
Best Answer

Answer by siddharth3322 · May 25, 2014 at 11:05 AM

I have found solution for this problem. Here I posted it to help other members.

 private bool isTouchDown;
     private float xOffset;
 
     void Update ()
     {
         if (Input.GetMouseButtonDown (0)) {
             Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             RaycastHit2D hit = Physics2D.Raycast (pos, Vector2.zero);
             if (hit != null && hit.collider != null && hit.collider.CompareTag (Constants.TAG_PLAYER_PADDLE)) {
                 isTouchDown = true;
                 Vector3 touchPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                 xOffset = transform.position.x - touchPosition.x;
             }
 
         } else if (Input.GetMouseButtonUp (0))
             isTouchDown = false;
 
         if (isTouchDown) {
             Vector3 touchPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             touchPosition.y = -8f;
             touchPosition.z = -1;
             touchPosition.x += xOffset;
             touchPosition.x = Mathf.Clamp (touchPosition.x, -3.5f, 3.5f);
             transform.position = touchPosition;
         }
     }

Now I can able to drag game object from any point in horizontal direction.

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
1

Answer by Magok_Stelios · May 25, 2014 at 11:38 AM

You can try something like that....

 private bool dragging = false;
 private float distance;
 void OnMouseDown()
 {
     distance = Vector3.Distance(transform.position, Camera.main.transform.position);
     dragging = true;
 }

 void OnMouseUp()
 {
     dragging = false;
 }
 void Update()
 {
     if (dragging)
     {
          Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          Vector3 rayPoint = ray.GetPoint(distance);   //
          rayPoint.y = -8f;
          rayPoint.z = -1f;
          transform.position = rayPoint;
     }
 }

i'm using soemthing similar for my projects and works like a cake!

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 siddharth3322 · May 25, 2014 at 03:17 PM 0
Share

Thanks for your suggestion.

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

Terrain Handling For Performance Improvement in 2D Game 0 Answers

Stretch sprite to a position (finger position) 1 Answer

Background Sprite Setting 0 Answers

GameObjects creation within boundry 3 Answers

Unity OnMouseDrag too fast for Update function 0 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