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!
(I’m not sure this works on iOS, I only use windows)
Perhaps what you are looking for is OnMouseDrag()?
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);
}
}