- Home /
How can you transfer mousePostion to mobile?
I have starting building a game but built it for PC and now i want to change it to mobile. However, i have a sprite that is only movable using the mouse so it follows the X position of your mouse. Not the Y (Im using Vector 2 so Z not included) I would like to know if there is a way so on mobile you can drag your finger across the screen and it would follow your finger rather than using a mouse. Here is the script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Paddle : MonoBehaviour
{
//Configuration Parameters
[SerializeField] float screenWidth = 16f;
[SerializeField] float minX = 1f;
[SerializeField] float maxX = 15f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float mousePos = Input.mousePosition.x / Screen.width * screenWidth;
Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
paddlePos.x = Mathf.Clamp(mousePos, minX, maxX);
transform.position = paddlePos;
}
}
Thank You.
Answer by Priyanka-Rajwanshi · Apr 25, 2020 at 04:59 PM
@Scorchpick Use Input.GetTouch(0).position in place of Input.mousePosition.
if(Input.touchCount> 0){
Vector3 touchPos = Input.GetTouch(0).position;
//Add rest code
}
This touchPos is equivalent to Input.mousePosition. Moreover, mousePosition too works on mobile.
Your answer
Follow this Question
Related Questions
Rts touch select unit 2 Answers
Instantiated an Prefab on a Spherical Object at mouse cursor position 3 Answers
Contains(Input.mousePosition) reacts to wrong rect 1 Answer
How do I reset the mousePosition relative to an object I click on? 1 Answer
How do I detect mouse inputs on sprites that are on a render texture? 2 Answers