-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWalk1
99 lines (60 loc) · 1.96 KB
/
Walk1
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
using UnityEngine;
using System.Collections;
public class Walk : MonoBehaviour {
bool facingRight = true;
Animator anim;
bool walledL = false;
bool walledR = false;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.02f;
public LayerMask whatIsGround;
public Transform wallCheckL;
public Transform wallCheckR;
float wallRadius = 0.02f;
public LayerMask whatIsWall;
public float jumpforce = 700f;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
grounded = Physics2D.OverlapCircle(groundCheck.position,groundRadius,whatIsGround);
anim.SetBool ("Ground", grounded);
walledL = Physics2D.OverlapCircle(wallCheckL.position,wallRadius,whatIsWall);
anim.SetBool ("Wall", walledL);
walledR = Physics2D.OverlapCircle(wallCheckR.position,wallRadius,whatIsWall);
anim.SetBool ("Wall", walledR);
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D>().velocity = new Vector2(0, GetComponent<Rigidbody2D>().velocity.y);
if(move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Update(){
if (grounded && Input.GetKeyDown (KeyCode.Z)) {
anim.SetBool ("Ground", false);
GetComponent<AudioSource> ().Play ();
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpforce));
}
if (walledL){
anim.SetBool("Wall", true);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 0));
}
if (walledR){
anim.SetBool("Wall", true);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 0));
Walk1 maxSpeed = 0;
}
}
void Flip (){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}