-
Notifications
You must be signed in to change notification settings - Fork 1
/
AttackScript.cs
82 lines (81 loc) · 3.21 KB
/
AttackScript.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using UnityEngine;
public class AttackScript : MonoBehaviour {
public GameObject spawnObj;
private GameObject swing;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (swing == null)
{
getDirectionOfMouse();
}
else
{
Destroy(swing);
getDirectionOfMouse();
}
}
}
private void getDirectionOfMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Get x
float x_difference = ray.direction.x - ray.origin.x;
// Get y
float y_difference = ray.direction.y - ray.origin.y;
// Compare the absolute values to find the greater one
if(Mathf.Abs(x_difference) > Mathf.Abs(y_difference))
{
if(x_difference < 0)
{
// Right swing
swing = Instantiate(spawnObj, transform.position, transform.rotation, transform);
swing.transform.Translate(transform.localScale.x*3/4, 0f, 0f);
} else if(x_difference > 0)
{
// Left swing
swing = Instantiate(spawnObj, transform.position, transform.rotation, transform);
swing.transform.Translate(transform.localScale.x * -3 / 4, 0f, 0f);
}
} else if(Mathf.Abs(x_difference) < Mathf.Abs(y_difference))
{
if (y_difference < 0)
{
// Up swing
swing = Instantiate(spawnObj, transform.position, transform.rotation, transform);
swing.transform.Translate(0f, transform.localScale.y * 3 / 4, 0f);
}
else if (y_difference > 0)
{
// Down swing
swing = Instantiate(spawnObj, transform.position, transform.rotation, transform);
swing.transform.Translate(0f, transform.localScale.y * -3 / 4, 0f);
}
} else if(x_difference > 0 && y_difference > 0)
{
//Lower Left - make it a left swing
swing = Instantiate(spawnObj, transform.position, transform.rotation, transform);
swing.transform.Translate(transform.localScale.x * -3 / 4, 0f, 0f);
} else if(x_difference > 0 && y_difference < 0)
{
//Upper Left - make it a up swing
swing = Instantiate(spawnObj, transform.position, transform.rotation, transform);
swing.transform.Translate(0f, transform.localScale.y * 3 / 4, 0f);
} else if(x_difference < 0 && y_difference > 0)
{
//Lower Right - make it a down swing
swing = Instantiate(spawnObj, transform.position, transform.rotation, transform);
swing.transform.Translate(0f, transform.localScale.y * -3 / 4, 0f);
} else if(x_difference < 0 && y_difference < 0)
{
//Upper Right - make it a right swing
swing = Instantiate(spawnObj, transform.position, transform.rotation, transform);
swing.transform.Translate(transform.localScale.x * 3 / 4, 0f, 0f);
}
else
{
// Do nothing, because the click occured on the player object
}
}
}