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
1
Question by MrBaskerville · Nov 13, 2012 at 12:11 PM · c#dragginginput.mouseposition

How to drag a GameObject without centering it on Mouse Cursor position?

I´m trying to make a E-reader for comics in Unity C#, but i´m having a lot of trouble creating the scroll feature for the pages. The problem is that the Page snaps to the mouse when you try to drag it, meaning that it centers on the mouse position making it less than smooth.

This is the code that activates when you start to drag, before that it checks whether it´s a swipe or a button press. I´ve been to the edges of the internet to find a solution, and i´ve tried a lot of things like adding the position of the initial mouseclickpoint. But i´m at a loss with this one.

 void SwipeIndexMenu ()
     {
         RaycastHit hit;                                                        //The RayCasting when you start to drag pages. 
         Ray ry = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ry, out hit, 1000)) {
             
             Debug.DrawRay (ry.origin, ry.direction * 1000, Color.grey, 10);
             if (hit.collider.gameObject.tag == "IndexMenu") {                //To prevent the menu from dissapearing if you swipe the wrong place.
             } else {
                 if (hit.collider.gameObject.tag == "ScrollMenuIndex") 
                     {
                     
                     var newmousePos = Input.mousePosition;
                     Vector3 currentposition = newmousePos + MouseOffsetposition;
                     GameObject PageTransformerIndexMenu = GameObject.FindGameObjectWithTag ("ScrollMenuIndex");
                     
                     hit.transform.position = new Vector3 (
                             PageTransformerIndexMenu.transform.position.x,
                             Camera.main.ScreenToWorldPoint (currentposition).y,
                             PageTransformerIndexMenu.transform.position.z);
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 hvilela · Nov 13, 2012 at 12:32 PM 0
Share

Please, format your code.

avatar image sparkzbarca · Nov 13, 2012 at 12:44 PM 0
Share

honestly you probably need to animate the pages.

avatar image PAEvenson · Nov 13, 2012 at 01:20 PM 0
Share

you need to move the page based on the difference between the y when first clicked. If you can wait a while until tonight, I can send you code how I dragged sliders for a game I made. I was using GUITextures but you should be able to see the concept atleast.

avatar image MrBaskerville · Nov 13, 2012 at 01:29 PM 0
Share

I´d be very grateful if you could do that :D.

Right now i´m trying a lot of things where i record the mouseclick position, but i have yet to figure out how to utilize this information properly. :)

1 Reply

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

Answer by PAEvenson · Nov 13, 2012 at 01:36 PM

Ok my buddy was kind enough to email it to me. Keep in mind this was designed for a horizontal slider for volume control on an android. There is code to handle mouse down and finger touch. You should be able to figure out what you need from it. Enjoy!

 using UnityEngine;
 using System.Collections;
 
 public class SliderScript : MonoBehaviour 
 {
     
     public float min_X;
     public float max_X;
     public string prefsString;
     private float m_Volume = 0.0f;
         
     float originalWidth = 1280.0f;  // define here the original resolution
     float originalHeight = 800.0f; // you used to create the GUI contents 
     Vector3 scale;
     private bool handleFingerInput = false;
     void Awake () 
     {
         m_Volume = PlayerPrefs.GetFloat(prefsString);
         float xpos = min_X + ((max_X - min_X) * m_Volume);
         gameObject.transform.position = new Vector3(xpos, gameObject.transform.position.y, gameObject.transform.position.z);
     }
     // Use this for initialization
     void Start () 
     {
         scale.x = Screen.width/originalWidth; // calculate hor scale
         scale.y = Screen.height/originalHeight; // calculate vert scale
         scale.z = 1;    
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(gameObject.transform.position.x <= min_X)
             {
                 gameObject.transform.position = new Vector3(min_X, gameObject.transform.position.y, gameObject.transform.position.z);
             }
             else if(gameObject.transform.position.x >= max_X)
             {
                 gameObject.transform.position = new Vector3(max_X, gameObject.transform.position.y, gameObject.transform.position.z);
             }
         
         if(Input.touchCount > 0)
         {
             for(int i = 0; i < Input.touchCount; i++)
             {
                 Vector2 inputPosition = Input.touches[i].position;
                 
                 if (Input.touches[i].phase == TouchPhase.Began )
                 {
                     
                     if(guiTexture.HitTest(inputPosition) == true)
                     {
                         handleFingerInput = true;
                     }
                 }
                 else if((Input.touches[i].phase == TouchPhase.Moved || Input.touches[i].phase == TouchPhase.Stationary) && (handleFingerInput == true))
                 {
                     float xpos = gameObject.transform.position.x;
                     xpos = inputPosition.x /  Screen.width;
                     if(xpos <= min_X)
                     {
                         xpos = min_X;
                     }
                     else if(xpos >= max_X)
                     {
                         xpos = max_X;
                     }
                     
                     gameObject.transform.position = new Vector3(xpos, gameObject.transform.position.y, gameObject.transform.position.z);
                     
                     m_Volume = (xpos - min_X) / (max_X - min_X);
                     
                     if(PlayerPrefs.GetFloat(prefsString) != m_Volume)
                     {
                         PlayerPrefs.SetFloat(prefsString, m_Volume);
                         PlayerPrefs.Save();
                     }
                 }
                 else
                 {
                     handleFingerInput = false;
                 }
                 
                 
             }
         }
         
     
     }
     
     void OnMouseDown()
     {
         StartCoroutine("HandleMouseDown");
     }
     
 
     IEnumerator HandleMouseDown()
     {
         while(Input.GetMouseButtonUp(0) == false)
         {
             Vector3 inputPosition = Input.mousePosition;
             float xpos = gameObject.transform.position.x;
             xpos = inputPosition.x / Screen.width;
             if(xpos <= min_X)
             {
                 xpos = min_X;
             }
             else if(xpos >= max_X)
             {
                 xpos = max_X;
             }
                 
             gameObject.transform.position = new Vector3(xpos, gameObject.transform.position.y, gameObject.transform.position.z);
             m_Volume = (xpos - min_X) / (max_X - min_X);
             
             if(PlayerPrefs.GetFloat(prefsString) != m_Volume)
             {
                 PlayerPrefs.SetFloat(prefsString, m_Volume);
                 PlayerPrefs.Save();
             }
             
             yield return null;
         }
     }
 }
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 MrBaskerville · Nov 15, 2012 at 12:32 PM 0
Share

Thanks a lot, this helped me solve the issues :D.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity2D : How can i change animation by dragging finger left or right on screen? 0 Answers

What's the difference between MonoBehavior OnMouseDrag and IDragHandler OnDrag 1 Answer

Making a bubble level (not a game but work tool) 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