- Home /
Sluggish relative drag
I am trying to move my player based on relative drag . Here is my script :
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class playerInput : MonoBehaviour , IPointerDownHandler,IDragHandler,IPointerUpHandler
{
private Vector3 startPos;
private Vector3 curPos;
GameObject player;
void Start(){
player = GameObject.FindWithTag ("Player");
}
void Update()
{
}
#region IPointerDownHandler implementation
public void OnPointerDown (PointerEventData eventData)
{
startPos = Camera.main.ScreenToWorldPoint (eventData.position);
}
#endregion
#region IDragHandler implementation
public void OnDrag (PointerEventData eventData)
{
curPos = Camera.main.ScreenToWorldPoint (eventData.position) - startPos;
player.transform.position = (player.transform.position + curPos)/2;
}
#endregion
#region IPointerUpHandler implementation
public void OnPointerUp (PointerEventData eventData)
{
}
#endregion
}
It works fine , but during initial drag it snaps to off location , after that the movement is responsive and nice . If i do
player.transform.position += curPos/40;
instead of
player.transform.position = (player.transform.position + curPos)/2;
Then it wont snap and will fine , will move relative to my finger etc but the movement is sluggish and slow . If i dont divide curPos by 40
then the character moves too fast . Not sure what to do , any help is appreciated .
Answer by abhijeet1001 · Dec 23, 2016 at 05:36 PM
I got relative drag to work ( this is sort of hacky way i guess but it works fine ) . What it does is , it lets u move the object based on relative drag, you do by moving your finger somwhere on the screen ( like in the game " Shooty skies ") .
using UnityEngine;
using System.Collections;
public class drag : MonoBehaviour
{
public GameObject player;
GameObject mainBody;
Vector3 tempPos;
void Start(){
player = GameObject.FindWithTag ("Player");
mainBody = player.transform.GetChild (0).gameObject;
}
void Update(){
if (Input.touchCount == 1 && Time.timeScale > 0.0f) {
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
tempPos = player.transform.position;
player.transform.position = Camera.main.ScreenToWorldPoint (Input.mousePosition);
player.transform.position = new Vector3 (player.transform.position.x, 0, player.transform.position.z);
mainBody.transform.position = tempPos;
}
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
player.transform.position = Camera.main.ScreenToWorldPoint (Input.mousePosition);
player.transform.position = new Vector3 (player.transform.position.x, 0, player.transform.position.z);
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) {
tempPos = mainBody.transform.position;
player.transform.position = tempPos;
mainBody.transform.position = player.transform.position;
}
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612103147im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object drag not moving at the same rate as the pointer 1 Answer
Problem with jumping in fps game 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers