public void Update MoveHandler void FixedUpdate MoveToTargetPosition T

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public void Update()
{
MoveHandler();
}
void FixedUpdate()
{
MoveToTargetPosition(Time.fixedDeltaTime);
}
private void MoveToTargetPosition(float deltaTime)
{
var step = deltaTime * speed;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
}
private void MoveHandler()
{
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
Ray ray = Camera.main.ScreenPointToRay(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
break;
case TouchPhase.Stationary:
case TouchPhase.Moved:
if (Physics.Raycast(ray, out RaycastHit hit ))
{
if(hit.collider != null && (hit.collider.tag == "Table" || hit.collider.tag == "Player"))
{
targetPosition = new Vector3(hit.point.x, 0, hit.point.z);
}
}
break;
case TouchPhase.Canceled:
case TouchPhase.Ended:
targetPosition = transform.position;
break;
}
} else
{
targetPosition = transform.position;
}
}