How to move the platform on the screen with svaypami in unity3d, or just move it to the place where the finger is (axis X)?

enter image description here

Here is the code for a simple button motion:

void FixedUpdate(){ float move = Input.GetAxis("Horizontal"); GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y); } 
  • 2
    There is a touch processing (unit) in the unit and these units have phases ..... docs.unity3d.com/ScriptReference/TouchPhase.html ........ I think you should follow this with the Moved phase and find out which ones The coordinates flew the touch ... (otherwise it could move along the Y axis) and already do what you want: move it to the same coordinates or just detect a finger movement, but leave the platform speed as before .... for example if the movement is a wheelbarrow it was more than 1.5f (in one direction or another from the place where it was before) - means moving the platform - Alexey Shimansky

2 answers 2

To find out the location of your finger, you should use the Input class. It has an array of Input.touches . Transfer points from an array using the Camera class using the SreenPointsToRay method. After that, transfer the data to RayCast .

Example

 using System.Linq; using UnityEngine; namespace MyProjectName.Input { using Input = UnityEngine.Input; /// <summary> /// Обработчик взаимодействий. /// </summary> public class InputManager : MonoBehaviour { private void Update() { if (Time.timeScale == 0f) return; Vector2[] inputPoints = new Vector2[0]; #if UNITY_EDITOR || UNITY_STANDALONE // Стационарный обработчик. (ПК). if (!Input.GetMouseButtonDown(0)) return; inputPoints = new Vector2[1]; inputPoints[0] = Input.mousePosition; #elif UNITY_ANDROID || UNITY_IOS || UNITY_TIZEN || UNITY_WP_8 || UNITY_WP_8_1 // Мобильный обработчик. inputPoints = new Vector2[Input.touchCount]; for (int i = 0; i < Input.touchCount; i++) inputPoints[i] = Input.touches[i].position; #endif if (inputPoints.Length == 0) return; ToRaycast(inputPoints); } /// <summary> /// Перевод точек в Raycats. /// </summary> private void ToRaycast(Vector2[] inputPoints) { RaycastHit raycast = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(inputPoint), Vector2.zero); // В дальнейшем можно использовать эти данные. } } } 
  • In the method. ToRaycast , you can carry out all necessary actions. - vmp1r3

Here svaypy NOT NEED.

Yes, and rake, in fact, too ...

  1. You need to block your dragging object on the Y axis.

  2. You need a Drag () implementation — for example, something like this:

     using UnityEngine; using System.Collections; [RequireComponent(typeof(BoxCollider2D))] public class Drag : MonoBehaviour { private Vector3 screenPoint; private Vector3 offset; void OnMouseDown() { offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); } void OnMouseDrag() { Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset; transform.position = Vector3.Lerp(transform.position, curPosition, Time.deltaTime); } } 

I'm not sure that the code will work as needed, but the idea of ​​the 100% approach should be just that.


Or the implementation of a dredge that will work only on the X axis of the phone screen.

It may also be useful to google by IBeginDragHandler, IDragHandler, IEndDragHandler : IBeginDragHandler, IDragHandler, IEndDragHandler

for more correct implementation.