-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovement_Script.cs
68 lines (50 loc) · 1.49 KB
/
Movement_Script.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
using UnityEngine;
public class Movement_Script : MonoBehaviour
{
void Start()
{
Camera cam = Camera.main;
}
//velocity for movement
Vector2 velocity = Vector2.zero;
Vector2 inputMove;
public float velocityJump;
public float acceleration = 2.0f;
public float deacceleration = 2.0f;
public float maxSpeed = 5.0f;
//velocity for jump
public Rigidbody2D rb;
public float jumpAmount = 35;
public float gravityScale = 10;
public float fallingGravityScale = 40;
void Movement()
{
inputMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0);
if (inputMove != Vector2.zero)
{
velocity += acceleration * Time.deltaTime * inputMove.normalized;
}
else
{
velocity = Vector2.Lerp(velocity, Vector2.zero, deacceleration * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * jumpAmount, ForceMode2D.Impulse);
}
if (rb.velocity.y >= 0)
{
rb.gravityScale = gravityScale;
}
else if (rb.velocity.y < 0)
{
rb.gravityScale = fallingGravityScale;
}
velocity = Vector2.ClampMagnitude(velocity, maxSpeed);
transform.position += (Vector3)velocity * Time.deltaTime;
}
void Update()
{
Movement();
}
}