blob: 25aaa723c34f4c142fabc64cd84b272741955657 (
plain)
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
53
54
55
56
57
58
59
|
using System;
using UnityEngine;
public class VirtualJoystick : MonoBehaviour, IVirtualJoystick
{
public Vector2 Delta { get; private set; }
public float InnerRadius = 0.64f;
public float OuterRadius = 1.28f;
public CircleCollider2D Outer;
public SpriteRenderer Inner;
private Controller myController = new Controller();
protected virtual void FixedUpdate()
{
this.myController.Update();
DragState dragState = this.myController.CheckDrag(this.Outer, false);
if (dragState - DragState.TouchStart <= 1)
{
float maxLength = this.OuterRadius - this.InnerRadius;
Vector2 vector = this.myController.DragPosition - base.transform.position;
float magnitude = vector.magnitude;
Vector2 a = new Vector2(Mathf.Sqrt(Mathf.Abs(vector.x)) * Mathf.Sign(vector.x), Mathf.Sqrt(Mathf.Abs(vector.y)) * Mathf.Sign(vector.y));
this.Delta = Vector2.ClampMagnitude(a / this.OuterRadius, 1f);
this.Inner.transform.localPosition = Vector3.ClampMagnitude(vector, maxLength) + Vector3.back;
return;
}
if (dragState != DragState.Released)
{
return;
}
this.Delta = Vector2.zero;
this.Inner.transform.localPosition = Vector3.back;
}
public virtual void UpdateJoystick(FingerBehaviour finger, Vector2 velocity, bool syncFinger)
{
Vector3 vector = this.Inner.transform.localPosition;
Vector3 vector2 = velocity.normalized * this.InnerRadius;
vector2.z = vector.z;
if (syncFinger)
{
vector = Vector3.Lerp(vector, vector2, Time.fixedDeltaTime * 5f);
this.Inner.transform.localPosition = vector;
vector = this.Inner.transform.position;
vector.z = -26f;
finger.transform.position = vector;
return;
}
if (this.Inner.gameObject != finger.gameObject)
{
this.Inner.transform.localPosition = vector2;
}
}
}
|