- Home /
How to get an object to follow touch when held. (move on y axis)
Hello! I'm trying to make a pong-like game and I need the player to be able to move the block to deflect the ball. I'm making this for iOS and I would like to be able to move it using just tapping and holding to move on the Y-axis. I've looked online and could not figure out what to use. I found something that works on PC, but not Mobile. Here is the script:
public class Paddle : MonoBehaviour
{
public float paddleSpeed = 1;
public Vector3 playerPos;
void Update()
{
float yPos = transform.position.y + (Input.GetAxis("Vertical") * paddleSpeed);
playerPos = new Vector3(-36, Mathf.Clamp(yPos, -11.58f, 11.58f), 0);
transform.position = playerPos;
}
}
What I think needs to be changed is the (Input.GetAxis("Vertical") * paddleSpeed)
. but I can't get it to work with GetMouseButton(0). P.S. I'm new here so I don't know if I have this in the right place. Anyways, thanks in advance!
Answer by BlastOffProductions · Dec 13, 2016 at 06:07 AM
Now I know this works.
using UnityEngine;
using System.Collections;
public class ClickDrag : MonoBehaviour { public GameObject character;
public float speed = 50;
void Update ()
{
if(Input.touchCount == 0)
{
Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.z,Input.mousePosition.y, 50.0f));
character.transform.Translate(Vector3.MoveTowards(character.transform.position, target, speed * Time.deltaTime) - character.transform.position);
}
}
}
I get no errors but it doesn't move when I click and hold the paddle.
Sorry, didn't give instructions. First put this script on your camera, then drag the paddle game object to the available public space on the script.
Here's an example.
Hmm, it could be when you pasted the code, you accidentally didn't delete all the old code first. This has happend to me a few times. Also, I noticed (from the video) that on the paddles rigidbody; the rotations are all frozen, and the Z position is frozen too. This might have something to do with it. Also the might what to check if you have multiple cameras in your scene. Because the script targets: (Camera.main.ScreenToWorldPoint), so multiple cameras active in the scene, might mess with the code.
Hope this helps, if it doesn't i'm still here.
I checked everything and nothing has changed. This is weird why this is happening. Is there anything you want me to show you?
Yes, I was wondering what is attached to the paddle before you press play.
That's what is attached to the paddle. I also tried moving the burger away and it continued to circle around and get bigger.
Answer by Laiken · Dec 18, 2016 at 12:45 AM
(I'm not sure this works on iOS, I only use windows)
Perhaps what you are looking for is OnMouseDrag()? https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDrag.html
You can make a flat collider (similar to a canvas, but instead of a canvas, it's a collider) on the whole screen and put it on a Layer called CanvasCollider, then when clicking and dragging with the mouse (not sure if it's the same for touch) the object will slide on the collider
(you need to put this on the object that will be moved)
void Start ()
{
canvasColliderMask = LayerMask.GetMask("CanvasCollider");
cam = Camera.main;
}
void OnMouseDrag()
{
RaycastHit hit;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100, canvasColliderMask))
{
transform.position = new Vector3(transform.position.x, hit.point.y, transform.position.z);
}
}
lol!
I don't know why you got those errors since I copied that from my own game and it gives me no errors. I guess I should not try to answer stuff related to another plataform. Sorry about that. :|
I will give you one last suggestion that will not take much time for you to test because it adds very little to your own code (just copy and paste it on your script):
public class Paddle : $$anonymous$$onoBehaviour
{
public float paddleSpeed = 1;
public Vector3 playerPos;
float yPosOnLastFrame;
void Update()
{
float yPosOffset = Input.mousePosition.y - yPosOnLastFrame;
yPosOnLastFrame = Input.mousePosition.y;
float yPos = transform.position.y + yPosOffset * paddleSpeed);
playerPos = new Vector3(-36, $$anonymous$$athf.Clamp(yPos, -11.58f, 11.58f), 0);
transform.position = playerPos;
}
}
if the movement is inverted, multiply yPosOffset by -1
It works! Although it's a little buggy, I'm sure I can find a way to make it on point with the touch. Anyways, thank you! (It doesn't let me upvote since I don't have 100 rep but if you reply to the main post, I'd be happy to make it the answer)
Your answer
![](https://koobas.hobune.stream/wayback/20220612102611im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
PlayStation Vita input 1 Answer
Input Snap option not returning 0? 0 Answers
input resets after scene change,Held Down Input not responding after scene change. 0 Answers
How to convert Input.GetAxis to Accelerometer control? 2 Answers