-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortSword.cs
72 lines (59 loc) · 1.99 KB
/
ShortSword.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShortSword : Weapon
{
bool attacking = false;
private Vector3 mousePos;
SpriteRenderer Image;
BoxCollider2D ObjectCollider;
void Start()
{
//set damage
transform.GetComponent<DamageOnCollision>().damage = PlayerStats.PlayerStatsSingle.strength;
transform.GetComponent<DamageOnCollision>().onCollide = onCollide;
Image = gameObject.GetComponentInChildren<SpriteRenderer>();
ObjectCollider = gameObject.GetComponentInChildren<BoxCollider2D>();
}
void FixedUpdate()
{
if (attacking)
{
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z - 8f);
}
}
public override void Attack()
{
if (!attacking)
{
StartCoroutine(SwordAttack());
}
}
IEnumerator SwordAttack()
{
//calculate angle for pointing at mouse
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var dir = mousePos - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
//offset angle where it starts so it swings aross player
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z + 60);
attacking = true;
Image.enabled = true;
ObjectCollider.enabled = true;
//change later to waiting for angle reached not time limit
yield return new WaitForSeconds(.25f);
Image.enabled = false;
ObjectCollider.enabled = false;
attacking = false;
}
//sword does nothing special on collide right now, but later it will; sounds, effects, etc.
void onCollide()
{
//
}
void OnDestroy()
{
PlayerController.PlayerControllerSingle.playerAttack -= Attack;
}
}