1 using UnityEngine;
2 using
Assets.Scripts;
3 using
UnityEngine.SceneManagement;
4
5
6 public
class CharacterRotateMovement : MonoBehaviour
7 {
8     
//character model found in https://www.assetstore.unity3d.com/en/#!/content/3012
9
10     
private Vector3 moveDirection = Vector3.zero;
11     
public float gravity = 20f;
12     
private CharacterController controller;
13     
private Animator anim;
14
15     
public float JumpSpeed = 8.0f;
16     
public float Speed = 6.0f;
17     
public Transform CharacterGO;
18
19     
bool isInSwipeArea;
20
21
22     IInputDetector inputDetector =
null;
23
24     
// Use this for initialization
25     
void Start()
26     {
27         moveDirection = transform.forward;
28         moveDirection = transform.TransformDirection(moveDirection);
29         moveDirection *= Speed;
30
31         UIManager.Instance.ResetScore();
32         UIManager.Instance.SetStatus(Constants.StatusTapToStart);
33
34         GameManager.Instance.GameState = GameState.Start;
35
36         anim = CharacterGO.GetComponent<Animator>();
37         inputDetector = GetComponent<IInputDetector>();
38         controller = GetComponent<CharacterController>();
39     }
40
41     
// Update is called once per frame
42     
void Update()
43     {
44         
switch (GameManager.Instance.GameState)
45         {
46             
case GameState.Start:
47                 
if (Input.GetMouseButtonUp(0))
48                 {
49                     anim.SetBool(Constants.AnimationStarted,
true);
50                     
var instance = GameManager.Instance;
51                     instance.GameState = GameState.Playing;
52
53                     UIManager.Instance.SetStatus(
string.Empty);
54                 }
55                 
break;
56             
case GameState.Playing:
57                 UIManager.Instance.IncreaseScore(
0.001f);
58
59                 CheckHeight();
60
61                 DetectJumpOrSwipeLeftRight();
62
63                 
//apply gravity
64                 moveDirection.y -= gravity * Time.deltaTime;
65                 
//move the player
66                 controller.Move(moveDirection * Time.deltaTime);
67
68                 
break;
69             
case GameState.Dead:
70                 anim.SetBool(Constants.AnimationStarted,
false);
71                 
if (Input.GetMouseButtonUp(0))
72                 {
73                     
//restart
74                     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
75                 }
76                 
break;
77             
default:
78                 
break;
79         }
80
81     }
82
83     
private void CheckHeight()
84     {
85         
if (transform.position.y < -10)
86         {
87             GameManager.Instance.Die();
88         }
89     }
90
91     
private void DetectJumpOrSwipeLeftRight()
92     {
93         
var inputDirection = inputDetector.DetectInputDirection();
94         
if (controller.isGrounded && inputDirection.HasValue && inputDirection == InputDirection.Top)
95         {
96             moveDirection.y = JumpSpeed;
97             anim.SetBool(Constants.AnimationJump,
true);
98         }
99         
else
100         {
101             anim.SetBool(Constants.AnimationJump,
false);
102         }
103
104
105         
if (GameManager.Instance.CanSwipe && inputDirection.HasValue &&
106          controller.isGrounded && inputDirection == InputDirection.Right)
107         {
108             transform.Rotate(
0, 90, 0);
109             moveDirection = Quaternion.AngleAxis(
90, Vector3.up) * moveDirection;
110             
//allow the user to swipe once per swipe location
111             GameManager.Instance.CanSwipe =
false;
112         }
113         
else if (GameManager.Instance.CanSwipe && inputDirection.HasValue &&
114          controller.isGrounded && inputDirection == InputDirection.Left)
115         {
116             transform.Rotate(
0, -90, 0);
117             moveDirection = Quaternion.AngleAxis(-
90, Vector3.up) * moveDirection;
118             GameManager.Instance.CanSwipe =
false;
119         }
120
121
122     }
123
124
125
126
127
128 }


Gõ tìm kiếm nhanh...