-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerTabTargeting.cs
More file actions
57 lines (51 loc) · 1.79 KB
/
PlayerTabTargeting.cs
File metadata and controls
57 lines (51 loc) · 1.79 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
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Mirror;
namespace uMMORPG
{
[RequireComponent(typeof(Player))]
[RequireComponent(typeof(PlayerIndicator))]
[DisallowMultipleComponent]
public class PlayerTabTargeting : NetworkBehaviour
{
[Header("Components")]
public Player player;
public PlayerIndicator indicator;
[Header("Targeting")]
public KeyCode key = KeyCode.Tab;
void Update()
{
// only for local player
if (!isLocalPlayer) return;
// in a state where tab targeting is allowed?
if (player.state == "IDLE" ||
player.state == "MOVING" ||
player.state == "CASTING" ||
player.state == "STUNNED")
{
// key pressed?
if (Input.GetKeyDown(key))
TargetNearest();
}
}
[Client]
void TargetNearest()
{
// find all monsters that are alive, sort by distance
// (NetworkIdentity.spawned is available on client too for those it sees)
// note: uses Linq, but this only happens on the client when pressing Tab
List<Monster> monsters = NetworkClient.spawned.Values
.Select(ni => ni.GetComponent<Monster>())
.Where(m => m != null && m.health.current > 0)
.ToList();
List<Monster> sorted = monsters.OrderBy(m => Vector3.Distance(transform.position, m.transform.position)).ToList();
// target nearest one
if (sorted.Count > 0)
{
indicator.SetViaParent(sorted[0].transform);
player.CmdSetTarget(sorted[0].netIdentity);
}
}
}
}