-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.cs
136 lines (103 loc) · 2.79 KB
/
Character.cs
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
133
134
135
136
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class Character :MonoBehaviour{
[SerializeField]
private float speed = 3.0F;
[SerializeField]
private float jumpforce = 3.0F;
[SerializeField]
public int score;
private bool isGrounded = false;
private Rigidbody2D PlayerRigidBody;
private Animator animator;
private SpriteRenderer Sprite;
public GUISkin MarkcSkin;
private CharState State
{
get { return (CharState)animator.GetInteger("State"); }
set { animator.SetInteger("State", (int)value); }
}
private void Awake()
{
PlayerRigidBody = GetComponentInChildren<Rigidbody2D>();
animator = GetComponentInChildren<Animator>();
Sprite = GetComponentInChildren<SpriteRenderer>();
}
private void Die()
{
if(transform.position.y<-20)
{
Application.LoadLevel(Application.loadedLevel);
}
}
public void OnGUI()
{
GUI.skin = MarkcSkin;
GUI.Label(new Rect(4, 0, 90, 70), "Marks: " + score);
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Marks")
{
score++;
Destroy(collision.gameObject);
}
}
private void FixedUpdate()
{
CheckGround();
}
private void Move()
{
Vector3 tempvector = Vector3.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + tempvector, speed * Time.deltaTime);
if(tempvector.x<0)
{
Sprite.flipX = true;
}
else
{
Sprite.flipX = false;
}
State = CharState.Go;
}
private void Start()
{
}
public void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Enemy")
Application.LoadLevel(Application.loadedLevel);
}
private void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3F);
isGrounded = colliders.Length > 1;
}
private void Jump()
{
State = CharState.Jump;
PlayerRigidBody.AddForce(transform.up*jumpforce,ForceMode2D.Impulse);
}
private void Update()
{
State = CharState.Stay;
if (Input.GetButton("Horizontal"))
{
Move();
}
if (isGrounded&&Input.GetButton("Jump"))
{
Jump();
}
Die();
}
public enum CharState
{
Stay,
Go,
Jump
}
}