forked from markkleeb/newarcade-spring2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week3-playerMovement.cs
73 lines (55 loc) · 1.55 KB
/
Week3-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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public KeyCode moveLeft;
public KeyCode moveRight;
public KeyCode jump;
public float speed = 10.0f;
public float jumpForce = 10.0f;
public Sprite jumpSprite;
public Sprite idleSprite;
private bool facingRight = true;
private SpriteRenderer crocSR;
private Rigidbody2D crocRB;
// Start is called before the first frame update
void Start()
{
crocSR = gameObject.GetComponent<SpriteRenderer>();
crocRB = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
//if (Input.GetKeyDown(moveLeft)) {
if(Input.GetAxisRaw("Horizontal") < 0) {
transform.Translate(Vector3.left * speed*Time.deltaTime);
facingRight = false;
}
//if (Input.GetKeyDown(moveRight))
if(Input.GetAxisRaw("Horizontal") > 0){
transform.Translate(Vector3.right * speed * Time.deltaTime);
facingRight = true;
}
if (Input.GetKeyDown(jump))
{
crocRB.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
if (crocRB.velocity.y < 0.5) {
crocSR.sprite = idleSprite;
}
else
{
crocSR.sprite = jumpSprite;
}
if (facingRight)
{
crocSR.flipX = false;
}
else
{
crocSR.flipX = true;
}
}
}