Есть джойстик, по которому происходит движение влево-вправо и по свайпу предусмотрен прыжок. По отдельности все работает, но невозможно и бежать, и прыгать одновременно. Вообще нет идей, как исправить это, прошу помощи)
C# |
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
| using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Player : MonoBehaviour
{
[SerializeField] private float speed = 3f;
[SerializeField] private int maxHealth = 5;
[SerializeField] private float jumpForce = 15f;
public int currentHealth;
private bool stor = true;
private bool isGrounded = false;
private bool isFacingRight = true;
private bool isSwiping;
private float groundRadius = 0.2f;
private float ddDelta = 80;
public LayerMask whatIsGround;
public Transform groundCheck;
public Rigidbody2D rb;
private SpriteRenderer sprite;
private Animator animator;
public Joystick joystick;
public static Player Instance { get; set; }
public static event OnSwipeInput SwipeEvent;
public delegate void OnSwipeInput(Vector2 direction);
private Vector2 impulser = new Vector2(-6, 2);
private Vector2 impulsel = new Vector2(6, 2);
private Vector2 tapPosition;
private Vector2 swipeDelta;
private void Start()
{
Player.SwipeEvent += Jump;
}
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
Instance = this;
}
private void FixedUpdate()
{
CheckGround();
if (joystick.Horizontal > 0 && !isFacingRight) Flip();
else if (joystick.Horizontal < 0 && isFacingRight) Flip();
}
public void Update()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase==TouchPhase.Began)
{
isSwiping = true;
tapPosition = Input.GetTouch(0).position;
}
else if (Input.GetTouch(0).phase == TouchPhase.Canceled || Input.GetTouch(0).phase == TouchPhase.Ended)
{
ResetSwipe();
}
}
CheckSwipe();
if (joystick.Horizontal != 0) Run();
}
private void Flip()
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
stor = !stor;
}
private void Run()
{
;
Vector3 dir = transform.right * joystick.Horizontal;
transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);
}
private void Jump(Vector2 direction)
{
if (direction==Vector2.up) {
rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
}
private void CheckGround()
{
Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, 0.3f);
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
}
private void CheckSwipe()
{
swipeDelta = Vector2.zero;
if (isSwiping)
{
if (Input.touchCount > 0)
swipeDelta = Input.GetTouch(0).position - tapPosition;
}
if (swipeDelta.magnitude > ddDelta)
{
if (SwipeEvent != null)
{
if (Mathf.Abs(swipeDelta.x) < Mathf.Abs(swipeDelta.y))
SwipeEvent(swipeDelta.y > 0 ? Vector2.up:Vector2.zero);
}
ResetSwipe();
}
}
private void ResetSwipe()
{
isSwiping = false;
tapPosition = Vector2.zero;
swipeDelta = Vector2.zero;
}
} |
|