-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
71 lines (53 loc) · 2.09 KB
/
Copy pathPlayerController.cs
File metadata and controls
71 lines (53 loc) · 2.09 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float jumpHeight;
public Transform groundCheck;
public float groundCheckRadius; //how big the circle
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
void FixedUpdate() {
//occurs a set amount of time every second (good for physics)
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update()
{
if(grounded) {
doubleJumped = false;
}
anim.SetBool("Grounded", grounded);
if(Input.GetKeyDown(KeyCode.Space) && grounded) {
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
}
if(Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded) {
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
doubleJumped = true;
}
if(Input.GetKey(KeyCode.D)) {
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if(Input.GetKey(KeyCode.A)) {
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
//moving to right
if(GetComponent<Rigidbody2D>().velocity.x > .1) {
transform.localScale = new Vector3(1f, 1f, 1f);
}
//moving to left
else if(GetComponent<Rigidbody2D>().velocity.x < -.1){
transform.localScale = new Vector3(-1f, 1f, 1f);
}
}
}