Home>

I want to touch and rotate

Error message
Debug.Log ("Begin");is not displayed
Do not go to touch processing
Applicable source code
using UnityEngine;
using System.Collections;
public class TouchController: MonoBehaviour
{
    ///<summary>Rotation target</summary>
    public GameObject image;
    ///<summary>Rotation speed</summary>
    public float Speed ​​= 0.01f;

    void Update ()
    {
        if (Input.touchCount>0)
        {
            Touch touch = Input.GetTouch (0);
            if (touch.phase == TouchPhase.Began)
            {
                // start touch
                Debug.Log ("Begin");
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                // touch movement
                Debug.Log ("Moved");
                // Angle calculation according to the amount of movement
                float xAngle = touch.deltaPosition.y * Speed ​​* 10;
                float yAngle = -touch.deltaPosition.x * Speed ​​* 10;
                float zAngle = 0;
                //rotation
                image.transform.Rotate (xAngle, yAngle, zAngle, Space.World);

            }
            else if (touch.phase == TouchPhase.Ended)
            {
                // end touch
                Debug.Log ("Ended");
            }
        }
    }
}

Please describe what you tried for the problem here.

Supplemental information (FW/tool version etc.)

Please provide more information here.

  • Answer # 1

    I tried to run this code on my iPhone, but it worked without any problems.

    -Does not work when running on Unity editor (Windows, Mac)
    As described below, Input.Touches will not work unless it is built for iPhone or Android and it is run on a real machine
    How to debug Input.Touches in the editor
    I think you should change the method according to your purpose
    ・ I want to build and run it on a real machine → Build it every time and check the operation, or use the following code that works on both the editor and the real machine
    Implement common iOS/Android/Editor touch processing in Unity
    ・ It is not necessary to run on iPhone or Android, it should work on Windows or Mac → Use Input.GetMouseButton, Input.GetMouseButtonDown methods

    I tried to write the code that works on the Unity editor (Windows, Mac) as a test, so check if it works here.

    using UnityEngine;
    public class TouchController: MonoBehaviour
    {
        ///Rotation target
        public GameObject image;
        ///Rotation speed
        public float Speed ​​= 0.1f;
        private Vector3 _previousMousePosition;
        void Update ()
        {
            if (Input.GetMouseButtonDown (0))
            {
                // start clicking
                Debug.Log ("Begin");
                // keep mouse coordinates of previous frame
                _previousMousePosition = Input.mousePosition;
            }
            else if (Input.GetMouseButton (0))
            {
                // Click to move
                Debug.Log ("Moved");
                // Get current mouse coordinates
                var currentMousePosition = Input.mousePosition;
                // find vector from previous frame mouse coordinates to current mouse coordinates
                var deltaMousePosition = _previousMousePosition-currentMousePosition;
                // Angle calculation according to the amount of movement
                // * 10 is deleted because it is unnecessary because it is only necessary to adjust Speed
                float xAngle = deltaMousePosition.y * Speed;
                float yAngle = -deltaMousePosition.x * Speed;
                float zAngle = 0f;
                //rotation
                image.transform.Rotate (xAngle, yAngle, zAngle, Space.World);
                // update mouse coordinates of previous frame to current mouse coordinates
                _previousMousePosition = currentMousePosition;
            }
            else if (Input.GetMouseButtonUp (0))
            {
                // end click
                Debug.Log ("Ended");
            }
        }
    }