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
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
-
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 operationCreate 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; } } } }
Related articles
- c # - touch judgment of unity object
- i want to save the object of the scene in unity2d, delete it once, and then display the object saved by the button event
- unity - multiple bullets will be fired with one touch of the "attack" button
- c # - i want to make the current angle of the object in unity the angle of the entered value (in world coordinates)
- [c #] in unity, i want to repeatedly move the cube object to the z axis, but a variable conversion error occurs
- unity - i want to move the object like frisbee, but draw a spiral
- c # - object duplication script in unity 2d
- [unity, c #] how to specify the top object
- Detailed explanation of ES6 object operation examples
- JS prototype object operation example analysis
- [unity] overlapping object drag and camera control_left click operation
- unity instantinate returns child object position to x: 0 y: 0
- unity - if you tap the object with ipointerclickhandler twice
- [unity] mouse overlap between object drag and context menu display selection
- c# - unity pohto (pun2) child object synchronization processing
- unity - i want to make the object semi-transparent
- i want to make the object of unity semi-transparent
- c# - unity photon2(pun2) child object cannot be acquired
- i want to attach contentsizefitter to the unity parent object
- i want to make a line drawn with unity into a 3d object
- c # - player production in unity (shooting game)
- c# - method detection by instantiated gameobjects
- [unity] i want to determine in what order multiple collision detection objects collided
- c# - i want to change the specified value of the y coordinate to stop each time one object is generated
- c# - unity2d script shouldn't be placed, but could not load symbol is displayed
- c# - i am making a shooting game
- [unity][c#] i can only jump at the edge of the ground
- c# - unity) i want to refer to applicationpersistentdatapath
- unity - i don't know how to attach a method and target button object to onclick from script
- c# - [unity] i want to call a variable whose value has been changed from another script
[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