- Home /
How to drag an object with touch.
Hi I am making a 2D Pong style game. At first I thought that mouse simulates touch movement but it seems that is not the case here. I wrote this code to move paddle by using mouse but it does not let me use it for touch on phone. I would like to have this code edited so that I can control my paddle with touch on phone. Android phone to be precise. I am a noob at C# and unity so please don't judge me.
using UnityEngine; using System.Collections;
public class Dragger : MonoBehaviour {
Vector3 mousePos;
Vector3 playerPos;
public float paddleSpeed = 1;
void Update() {
mousePos = Vector3.Lerp (Input., playerPos, Time.deltaTime * paddleSpeed);
playerPos = new Vector3 (-22, Mathf.Clamp (mousePos.y, -12.5f, 12.5f), 0);
gameObject.transform.position = new Vector3 (-22, playerPos.y, 0);
}
}
P.s not all of this code is mine as I already told that I am anoob at C#
Answer by imilanspinka · Aug 02, 2015 at 10:31 PM
Okay, I am not going to try this since I have no Android SDKs installed, but I will try to write a code and you have to test if it works:
using UnityEngine;
using System.Collections;
public class PaddleControl : MonoBehaviour
{
private Touch initialTouch = new Touch();
private float distance = 0;
private bool hasSwiped = false;
GameObject paddle;
void Start()
{
paddle = FindGameObjectWithName ("paddle (whatever)");
//or FindGameObjectWithTag (...);
}
void FixedUpdate()
{
foreach (Touch t in Input.touches)
{
if(t.TouchPhase == TouchPhase.Began)
{
initialTouch = t;
}
else if(t.TouchPhase == TouchPhase.Moved)
{
float deltaX = initialTouch.position.x - t.position.x;
float deltaY = initialTouch.position.y - t.position.y;
distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
if (distance > 100f)
{
paddle.transform.position.x += deltaX;
hasSwiped = true;
}
}
else if(t.TouchPhase == TouchPhase.Ended)
{
initialTouch = new Touch();
hasSwiped = false;
}
}
}
}
I am also a C# noobie so I am not sure if this would work, but try it, and if other members want to correct my code, they can. :)
EDIT: I think the code at line 34 is not going to work, if so, try instead:
if (distance > 100f)
{
//If you want the paddle to follow the mouse on X and Y, do:
Vector3 paddlePositionXY = new Vector3 (deltaX, deltaY, 0);
paddle.transform.position = paddlePositionXY;
//If you don't want the paddle to follow the mouse on Y, do:
Vector3 paddlePositionX = new Vector3 (deltaX, 0, 0);
paddle.transform.position = paddlePositionX;
hasSwiped = true;
}
Answer by iDownward_ · Jan 27, 2017 at 07:49 PM
@Firzok, write this code: void Update(){ float posY = gameObject.transform.position.y + (Input.GetAxis ("MouseY") * speed); playerPos = new Vector2 (transform.position.x, Mathf.Clamp (posY, minY, maxY)); gameObject.transform.position = playerPos; }
After that, don't forget to click File -> Build Settings -> Android -> Switch Platform.
Your answer
Follow this Question
Related Questions
Drag and DoubleTap from mouse to touch??? 0 Answers
Android Multi touches not working 1 Answer
How to Raycast on Touch? 3 Answers
it does not wait for user to touch,unity does not wait for me to touch a game object 0 Answers
The type or namespace name `Events' does not exist in the namespace `TouchScript' 1 Answer