Home>

I want to run a sample of the net that turns an object by touch operation
Hierachy Cube-->I'm trying to Drag to Inspector's TouchController
I'm not going well.
Therefore, even if executed, it does not come to the processing of void Update () of TouchController.cs.

URL


Applicable source code
using UnityEngine;
using System.Collections;
using System.Linq;
public class TouchController: MonoBehaviour
{
    ///<summary>Rotation target</summary>
    public GameObject Cube;
    ///<summary>Rotation speed</summary>
    public float Speed ​​= 0.01f;
    void Update ()
    {
        // Touch count acquisition (Linq was used)
        int touchCount = Input.touches
            .Count (t =>t.phase! = TouchPhase.Ended&&t.phase! = TouchPhase.Canceled);
        if (touchCount == 1)
  • Answer # 1

    [Unity] Rotate object with touch operation

    I tried to execute it with reference to the above article on which tamachan2020 was posted, but it worked, so I will describe the possible cause.

    Cannot run 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 and Mac → Use Input.GetMouseButton and Input.GetMouseButtonDown methods

    Building for iPhone or Android and running it on a real machine, but doesn't work
    There is a possibility that setup has failed, check if there is an error in red from Windows>General>Console of Unity Editor, or paste a screenshot of what you did at the following site If you can, I will investigate again.
    [Unity] Touch object to rotate object

  • Answer # 2

    Let's solve one by one until you can check if Update is working

    1 Place a Cube
    >I don't see any wooden squares like the ones currently on the net

    This is right-click in Hierarchy view and select 3D Object>Cube to place the Cube, it is not a wooden square like the site but it is enough to check the operation

    Create a TouchController script similar to 2 sites


    Write the site code in the created TouchController.cs

    * It does not work unless the name of the created script and the class name of the described code are TouchController

    Attach the 3 TouchController component to an empty game object

    You can drag and drop TouchController.cs from the Project view into the Inspector view, but this time we are attaching from the Add Component in the Inspector view

    If there is something you can't do so far, you can recreate a new Unity project and start over from scratch

    4 How to check if the script update is working
    >Because it doesn't stop at the beginning of update in script
    Although tamachan2020 has been confirmed to work with breakpoints, it is possible that the debugging method does not match and is not working, so let's debug this time with a code base.
    Change the code of TouchController.cs as follows and check whether the log is displayed in the Console view.

    using UnityEngine;
    using System.Linq;
    public class TouchController: MonoBehaviour
    {
        ///Rotation target
        public GameObject Cube;
        ///Rotation speed
        public float Speed ​​= 0.01f;
        void Update ()
        {// Touch count acquisition (Linq was used)
            int touchCount = Input.touches
                .Count (t =>t.phase! = TouchPhase.Ended&&t.phase! = TouchPhase.Canceled);
            // Display the number of touches in the log that can be checked from Windows>General>Console of the processing that was added for debugging, Unity editor
            Debug.Log ($"Number of touches: {touchCount}");
            if (touchCount == 1)
            {
                Touch t = Input.touches.First ();
                switch (t.phase)
                {
                    case TouchPhase.Moved:
                        // Angle calculation according to the amount of movement
                        float xAngle = t.deltaPosition.y * Speed ​​* 10;
                        float yAngle = -t.deltaPosition.x * Speed ​​* 10;
                        float zAngle = 0;
                        //rotation
                        Cube.transform.Rotate (xAngle, yAngle, zAngle, Space.World);
                        break;
                }
            }
        }
    }