How to implement such control as on video? Game: rolly vortex
Video -
How to achieve such control? What should I use? help me please
My code (edited):
public class TouchControl : MonoBehaviour { float level_width = 1f; float speed = 0.01f; void Update() { if(Input.touchCount > 0) { Debug.Log("Result = " + ((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2); if(((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2 > 0) { Debug.Log(((Input.GetTouch(0).position.x + 0.5f) / 2) + " result"); MoveLeft(); } if (((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2 < 0) { Debug.Log(((Input.GetTouch(0).position.x + 0.5f) / 2) + " result"); MoveRight(); } } } void MoveLeft() { Vector3 newPos = transform.position; newPos.x = (Input.GetTouch(0).position.x * level_width) * - 1; transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime); } void MoveRight() { Vector3 newPos = transform.position; newPos.x = Input.GetTouch(0).position.x * level_width; transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime); } } 1 Answer
Something like this should get you started:
public float level_width = 5f; public float speed = 0.1f; // 0..1 if(Input.touchCount > 0) { Vector3 newPos = transform.position; newPos.x = Input.GetTouch(0).position.x * width; transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime) } speed will adjust the time it takes for the ball to reach the touch position.
level_width helps to scale the input from screen coordinates to your world/level.
Edit: I simplified your code a bit:
public class TouchControl: MonoBehaviour { float level_width = 10f; float speed = 0.01 f; float target_xpos = 0f; void Update() { if (Input.touchCount > 0) { target_xpos = ((Input.GetTouch(0).position.x / Screen.width) - 0.5 f) * 2 * level_width; MoveToFinger(); } } void MoveToFinger() { Vector3 newPos = transform.position; newPos.x = target_xpos; transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime); } } 11