-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerMovement.cs
74 lines (61 loc) · 1.84 KB
/
PlayerMovement.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody2D playerRb;
public float speed;
public float input;
public SpriteRenderer spriteRenderer;
public float jumpForce;
public LayerMask groundLayer;
private bool isGrounded;
public Transform feetPosition;
public float groundCheckCircle;
public float jumpTime = 0.35f;
public float jumpTimeCounter;
private bool isJumping;
[SerializeField] private AudioSource jumpSoundEffect;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
input = Input.GetAxisRaw("Horizontal");
if(input<0)
{
spriteRenderer.flipX = false;
}
else if (input>0){
spriteRenderer.flipX = true;
}
isGrounded = Physics2D.OverlapCircle(feetPosition.position, groundCheckCircle, groundLayer);
if(Input.GetButtonDown("Jump") && isGrounded == true)
{
jumpSoundEffect.Play();
jumpTimeCounter = jumpTime;
playerRb.velocity = Vector2.up*jumpForce;
isJumping = true;
}
if(Input.GetButton("Jump") && isJumping == true)
{
if(jumpTimeCounter>0)
{
playerRb.velocity = Vector2.up*jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if(Input.GetButtonUp("Jump")){
}
}
void FixedUpdate()
{
playerRb.velocity = new Vector2 (input*speed, playerRb.velocity.y);
}
}