SHOW:
|
|
- or go back to the newest paste.
1 | using System.Collections; | |
2 | using System.Collections.Generic; | |
3 | using UnityEngine; | |
4 | using UnityEngine.Events; | |
5 | ||
6 | ||
7 | public class Movement : MonoBehaviour | |
8 | { | |
9 | //variables | |
10 | ||
11 | public Animator anim; // sets variable for animator | |
12 | private bool movingleft; //anim bool | |
13 | private bool movingright;//anim bool | |
14 | private bool movingup;//anim bool | |
15 | private bool movingdown;//anim bool | |
16 | public float movespeed; //movement speed slider | |
17 | ||
18 | //edited by dlich | |
19 | public float movementSpeed = 1f; | |
20 | private Vector2 oldPos; | |
21 | private Vector2 newpos;// new position for movement destination | |
22 | private float lerpValue = 0; | |
23 | public enum State{idle, movement}; | |
24 | public State state = State.idle; | |
25 | ||
26 | void Start() //start of game trigger | |
27 | { | |
28 | anim = GetComponent<Animator>(); //get the animator for the player object | |
29 | } | |
30 | ||
31 | ||
32 | void FixedUpdate() //fixed update runs in sync with the physics machine, which keeps fps good | |
33 | - | newpos = transform.position; //grabs current position |
33 | + | |
34 | - | newpos.y = newpos.y + 5; // increments y by 5 (moving up by 5) |
34 | + | |
35 | - | transform.position = Vector2.Lerp(transform.position, newpos, 1); //desired outcome => move character from its current position to the newpos slowly |
35 | + | if(state == State.idle) |
36 | { | |
37 | if(HandleInput()) | |
38 | { | |
39 | - | newpos.x = newpos.x - 5; |
39 | + | lerpValue = 0; |
40 | - | transform.position = Vector2.Lerp(transform.position, newpos, 1); |
40 | + | oldPos = transform.position; |
41 | state = State.movement; | |
42 | } | |
43 | } | |
44 | if(state == State.movement) | |
45 | - | transform.position = Vector2.Lerp(transform.position, newpos, 1); |
45 | + | |
46 | if(lerpValue < 1) | |
47 | { | |
48 | transform.position = Vector2.Lerp(oldPos, newpos, lerpValue); | |
49 | lerpValue += Time.deltatime * movementSpeed | |
50 | - | transform.position = Vector2.Lerp(transform.position, newpos, 1); |
50 | + | } |
51 | else state = State.idle; | |
52 | } | |
53 | HandleAnimation(); | |
54 | } | |
55 | ||
56 | //added by dlich | |
57 | public bool HandleMovement() | |
58 | { | |
59 | newpos = transform.position; | |
60 | if (Input.GetKeyDown(KeyCode.W))//move up | |
61 | { | |
62 | newpos.y = newpos.y + 5; // increments y by 5 (moving up by 5) | |
63 | return true; | |
64 | } | |
65 | if (Input.GetKeyDown(KeyCode.A))// move left | |
66 | { | |
67 | newpos.x = newpos.x - 5; | |
68 | return true; | |
69 | } | |
70 | if (Input.GetKeyDown(KeyCode.D))// move right | |
71 | { | |
72 | newpos.x = newpos.x + 5; | |
73 | return true; | |
74 | } | |
75 | if (Input.GetKeyDown(KeyCode.S)) // move down | |
76 | { | |
77 | newpos.y = newpos.y - 5; | |
78 | return true; | |
79 | } | |
80 | return false; | |
81 | } | |
82 | ||
83 | //////////////////////////////////////////////////////////////// | |
84 | ////Animation | |
85 | //////////////////////////////////////////////////////////////// | |
86 | HandleAnimation() | |
87 | { | |
88 | movingleft = Input.GetKey(KeyCode.A); | |
89 | movingright = Input.GetKey(KeyCode.D); | |
90 | movingup = Input.GetKey(KeyCode.W); | |
91 | movingdown = Input.GetKey(KeyCode.S); | |
92 | if (movingleft) | |
93 | { | |
94 | anim.SetBool("moving_left", true); | |
95 | } | |
96 | if (!movingleft) | |
97 | { | |
98 | anim.SetBool("moving_left", false); | |
99 | } | |
100 | ||
101 | - | |
101 | + | |
102 | if (movingright) | |
103 | - | |
103 | + | |
104 | anim.SetBool("moving_right", true); | |
105 | } | |
106 | - | } |
106 | + | |
107 | { | |
108 | anim.SetBool("moving_right", false); | |
109 | } | |
110 | ||
111 | // | |
112 | if (movingup) | |
113 | { | |
114 | anim.SetBool("moving_up", true); | |
115 | } | |
116 | if (!movingup) | |
117 | { | |
118 | anim.SetBool("moving_up", false); | |
119 | } | |
120 | ||
121 | // | |
122 | if (movingdown) | |
123 | { | |
124 | anim.SetBool("moving_down", true); | |
125 | } | |
126 | if (!movingdown) | |
127 | { | |
128 | anim.SetBool("moving_down", false); | |
129 | } | |
130 | } | |
131 | } |