-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerMovementScript
96 lines (72 loc) · 1.76 KB
/
PlayerMovementScript
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
public Animator playerAnimator;
Rigidbody2D rb2d;
respawn repawn;
public float runSpeed = 80f;
public float horizontalMove;
bool jump;
public bool crouch;
public bool canTakeDamage = true;
float prevVelY;
float currVelY;
int jumpState;
private void Start()
{
rb2d = GetComponent<Rigidbody2D>();
repawn = GetComponent<respawn>();
crouch = false;
}
// Update is called once per frame
void Update()
{
if (repawn.alive)
{
//Tager Input
Physics();
}
}
void FixedUpdate()
{
if (repawn.alive)
{
//Bruger Input
PhysicsFixed();
}
}
void Physics()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
playerAnimator.SetFloat("speed", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
playerAnimator.SetBool("IJ", true);
playerAnimator.SetTrigger("jumpAround");
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
}
else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.layer == 10)
{
playerAnimator.SetBool("IJ", false);
}
}
void PhysicsFixed()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}