Question by
TheGardenofEthan · Feb 04, 2020 at 08:57 AM ·
movementmobile
How can I make an object follow and stay where my finger is moving?
At the moment the cube I am wanting to move follows the touch of my finger but very slowly. I want it to essentially be glued to my finger so that movement is based on the user's real time finger position.
public class MovebyDrag : MonoBehaviour { private Touch touch; public float speed;
// Start is called before the first frame update
void Start()
{
speed = 10f;
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
{
Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y,10));
transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime);
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
c# Mobile Joystick issues - implementing joystick controls into movement and camera script 0 Answers
Move the player by holding a UI Button? 1 Answer
Trouble with cross platform input in mobile multiplayer 1 Answer
My character carries on the run animation for a few seconds even after standing still 0 Answers
Variable value does not change 0 Answers