/
Player4.cs
150 lines (138 loc) · 3.8 KB
/
Player4.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
137
138
139
140
141
142
143
144
145
146
147
148
using UnityEngine;
using System.Collections;
public class Player4 : MonoBehaviour {
public float maxAngularVelocity = 10;
public float drag = 0.9f;
public float dashForce = 300;
public float moveForce = 25;
public float rotateForce = 3;
public int maxRotateAngle = 30;
private bool up,down,left,right;
private void FixedUpdate () {
GetInpute();
Move();
Rotate();
}
private float fixPos(float p) {
if(p != 0){
return p * drag;
}else{
return p;
}
}
private void GetInpute(){
if(BuildTypeData.buildType == BuildType.PC){
if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) {
up = true;
}else{
up = false;
}
if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) {
left = true;
}else{
left = false;
}
if(Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) {
down = true;
}else{
down = false;
}
if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) {
right = true;
}else{
right = false;
}
}else if(BuildTypeData.buildType == BuildType.VITA){
if(Input.GetAxisRaw("Vertical")>0.9f) {//up
up = true;
}else{
up = false;
}
/*if(Input.GetKey(KeyCode.JoystickButton9)) {//right
right = true;
}else{
right = false;
}*/
if(Input.GetAxisRaw("Vertical")<-0.9f) {//down
down = true;
}else{
down = false;
}
/*if(Input.GetKey(KeyCode.JoystickButton11)) {//left
left = true;
}else{
left = false;
}*/
}
}
private void Rotate(){
float rotation = transform.localRotation.eulerAngles.z;
int xDirection = (int)transform.localScale.x;
if(up) {
if(xDirection>0){
if(rotation>(360-maxRotateAngle)||rotation<180){
transform.Rotate(0,0,-2*xDirection);
}
}else if(rotation<maxRotateAngle||rotation>180){
transform.Rotate(0,0,-2*xDirection);
}
}else if(down) {
if(xDirection>0){
if(rotation<maxRotateAngle||rotation>180){
transform.Rotate(0,0,2*xDirection);
}
}else{
if(rotation>(360-maxRotateAngle)||rotation<180){
transform.Rotate(0,0,2*xDirection);
}
}
}else{
if(transform.localRotation.eulerAngles.z>180&&transform.localRotation.eulerAngles.z<358){
transform.Rotate(0,0,1.9f);
}else if(transform.localRotation.eulerAngles.z>2&&transform.localRotation.eulerAngles.z<180) {
transform.Rotate(0,0,-1.9f);
}
}
}
private void Move(){
float newX = 0, newY = 0;
/*if(up) {//up
rigidbody2D.AddForce(new Vector3(0, moveForce, 0));
}
if(right) {//right
rigidbody2D.AddForce(new Vector3(moveForce, 0, 0));
}
if(down) {//down
rigidbody2D.AddForce(new Vector3(0, -moveForce, 0));
}
if(left) {//left
rigidbody2D.AddForce(new Vector3(-moveForce, 0, 0));
}*/
rigidbody2D.AddForce(new Vector3(Input.GetAxisRaw("Horizontal")*moveForce, Input.GetAxisRaw("Vertical")*moveForce, 0));
newX = fixPos(rigidbody2D.velocity.x);
newY = fixPos(rigidbody2D.velocity.y);
if(newX > 0.1 && transform.localScale.x == 1) {
Vector3 scale = transform.localScale;
scale.x = -1;
transform.localScale = scale;
float rot = transform.localEulerAngles.z;
transform.localEulerAngles = new Vector3(0,0,(360-rot));
} else if(newX < -0.1 && transform.localScale.x == -1) {
Vector3 scale = transform.localScale;
scale.x = 1;
transform.localScale = scale;
float rot = transform.localEulerAngles.z;
transform.localEulerAngles = new Vector3(0,0,(360-rot));
}
rigidbody2D.velocity = new Vector2(newX, newY);
}
//buildtype Display
/*void OnGUI()
{
if(BuildTypeData.buildType == BuildType.PC){
GUILayout.Box("BuildType PC");
}else if(BuildTypeData.buildType == BuildType.VITA){
GUILayout.Box("BuildType VITA");
}
}*/
}