- Home /
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);
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.
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. :)
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;
}
}
}
Your answer
