-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
85 lines (68 loc) · 2.27 KB
/
Copy pathPlayerController.cs
File metadata and controls
85 lines (68 loc) · 2.27 KB
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
75
76
77
78
79
80
81
82
83
84
85
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D player;
public Camera cam;
public Text text;
Vector2 movement;
Vector2 mousePosition;
Chest chest;
public Transform firePoint;
public GameObject bulletPrefab;
private GameObject chestObj;
private bool canShoot = false;
private bool canCollect = false;
public float bulletForce = 20f;
public float moveSpeed = 5f;
void Start() {
chestObj = GameObject.FindGameObjectWithTag("chest");
chest = chestObj.GetComponent<Chest>();
text.enabled = false;
}
void Update()
{
movement.x = Input.GetAxis("Horizontal");
movement.y = Input.GetAxis("Vertical");
mousePosition = cam.ScreenToWorldPoint(Input.mousePosition);
if (canCollect) {
text.enabled = true;
} else {
text.enabled = false;
}
if (Input.GetButtonDown("Fire1") && canShoot) {
Shoot();
}
if (canCollect && Input.GetKeyDown(KeyCode.Space)) {
getFirstWeapon();
// Show get weapon message + How to shoot tutorial
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "chest") {
canCollect = true;
}
}
void OnTriggerExit2D(Collider2D other) {
canCollect = false;
}
void getFirstWeapon() {
chest.setCollected(true);
this.canShoot = true;
}
void FixedUpdate() {
player.MovePosition(player.position + movement * moveSpeed * Time.fixedDeltaTime);
Vector2 lookDirection = mousePosition - player.position;
float angle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
player.rotation = angle;
}
void Shoot() {
Vector2 shootingDirection = mousePosition - player.position;
shootingDirection.Normalize();
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity);
bullet.GetComponent<Rigidbody2D>().velocity = shootingDirection * bulletForce;
bullet.transform.Rotate(0, 0, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
}
}