-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cs
More file actions
66 lines (55 loc) · 1.54 KB
/
Player.cs
File metadata and controls
66 lines (55 loc) · 1.54 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
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerController))]
[RequireComponent(typeof(GunController))]
public class Player : LivingEntity
{
public float moveSpeed = 5;
public Crosshairs crosshairs;
Camera viewCamera;
PlayerController controller;
GunController gunController;
protected override void Start ()
{
base.Start();
controller = GetComponent<PlayerController>();
gunController = GetComponent<GunController>();
viewCamera = Camera.main;
}
void Update ()
{
// Movement input
var moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
var moveVelocity = moveInput.normalized * moveSpeed;
controller.Move(moveVelocity);
// Look input
var ray = viewCamera.ScreenPointToRay(Input.mousePosition);
var groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight);
float rayDistance;
if (groundPlane.Raycast(ray, out rayDistance))
{
var point = ray.GetPoint(rayDistance);
//Debug.DrawLine(ray.origin, point, Color.red);
controller.LookAt(point);
crosshairs.transform.position = point;
crosshairs.DetectTargets(ray);
if ((new Vector2(point.x, point.z) - new Vector2(transform.position.x, transform.position.z)).sqrMagnitude > 1)
{
gunController.Aim(point);
}
}
// Weapon input
if (Input.GetMouseButton(0))
{
gunController.OnTriggerHold();
}
if (Input.GetMouseButtonUp(0))
{
gunController.OnTriggerRelease();
}
if (Input.GetKeyDown(KeyCode.R))
{
gunController.Reload();
}
}
}