-
Notifications
You must be signed in to change notification settings - Fork 0
/
Movement2d.cs
52 lines (47 loc) · 1.51 KB
/
Movement2d.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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement2d : MonoBehaviour
{
private Player_Base playerBase;
private Rigidbody2D rigidbody2D;
private BoxCollider2D boxCollider2D;
private void Awake()
{
playerBase = gameObject.GetComponent<Player_Base>();
rigidbody2D = transform.GetComponent<Rigidbody2D>();
boxCollider2D = transform.GetComponent<BoxCollider2D>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
float jumpvelocity = 100f;
rigidbody2D.velocity = Vector2.up * jumpvelocity;
}
}
private void FixedUpdate()
{
float moveSpeed = 40f;
rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
if (Input.GetKey(KeyCode.LeftArrow))
{
rigidbody2D.velocity = new Vector2(-moveSpeed , rigidbody2D.velocity.y);
}
else
{
if (Input.GetKey(KeyCode.RightArrow))
{
rigidbody2D.velocity = new Vector2(+moveSpeed , rigidbody2D.velocity.y);
}
else
{
//no keys pressed
rigidbody2D.velocity = new Vector2(0 , rigidbody2D.velocity.y);
rigidbody2D.constraints =
RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
}
}
}
}