Detect last touch for the next frame

Hi,

My scripts allows me to rotate the player-object on the Z-Axis with horizontal touches. The problem here is when I rotate the object and release my finger on the left side from the screen and touch it somwhere in the right side the object starts automatically from the right side, see the video below.

I want that the object should stays at the last touch position even if I touch the screen somewehre else again to move it more to the same side or to the other side. Can someone help me out?

Here is the video from this issue:

Script:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public GameObject showTouch;

    private Vector2 lastTouchPos;

    void FixedUpdate () {
        if (Input.touchCount > 0)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
            }
           
            else if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                /////
                showTouch.SetActive(true);
                showTouch.transform.position = Input.GetTouch(0).position;
                /////
                Vector2 position = Input.GetTouch(0).position;
               
                if (position.x < 95f)
                {
                    position.x = 95f;
                }
                else if (position.x > 265f)
                {
                    position.x = 265f;
                }

                transform.localRotation = Quaternion.Euler(0, 0, position.x);
            }
           
            else if (Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                this.lastTouchPos = Input.GetTouch(0).position; //and then?
                /////
                showTouch.SetActive(false);
                /////
            }
        }
    }
}

bump

You are using the position as an angle, which seems a bit strange. If you are looking to rotate the game object, you want something like this:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    public GameObject showTouch;

    private Vector2 lastTouchPos;

    private float angle = 0;

    void FixedUpdate()
    {
        if (Input.touchCount > 0)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
            }

            else if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                /////
                showTouch.SetActive(true);
                showTouch.transform.position = Input.GetTouch(0).position;
                /////

                angle -= Input.GetTouch(0).deltaPosition.x;

                transform.localRotation = Quaternion.Euler(0, 0, angle);
            }

            else if (Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                this.lastTouchPos = Input.GetTouch(0).position; //and then?
                /////
                showTouch.SetActive(false);
                /////
            }
        }
    }
}
1 Like

Thank you so much. With just a little tweaking it worked perfectly as I want.