-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDualStickController.cs
70 lines (52 loc) · 2.23 KB
/
DualStickController.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
using UnityEngine;
using System.Collections;
public class DualStickController : MonoBehaviour {
public float moveSpeed = 1.5f;
public float shootSpeed = 1f;
public GameObject projectile;
public float shootDelay = 0.5f;
[Header("Extendable Options")]
public GameObject[] _projectilePool;
private Vector2 moveDirection;
private Vector2 shootDirection;
private Vector2 position;
private bool canFireWeapon = true;
//Enable the weapon
void enableWeaponFire()
{
canFireWeapon = true;
}
//Disable the weapon
void disableWeaponFire()
{
canFireWeapon = false;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Get Vector2 direction of movement
moveDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
//Convert Vector3 to Vector2 for 2D
position = new Vector2(transform.position.x, transform.position.y);
//Move by <Position> + <Direction> * <Speed> * <Time>
rigidbody2D.MovePosition(position + moveDirection * moveSpeed * Time.deltaTime);
//If any of the two inputs have any data and the weapon can be fired
if ((Input.GetAxis("FireHorizontal") != 0.0f || Input.GetAxis("FireVertical") != 0.0f) && canFireWeapon)
{
//Convert the keys pressed to a Vector2
shootDirection = new Vector2(Input.GetAxis("FireHorizontal"), Input.GetAxis("FireVertical"));
//Calculate the angle to rotate the sprite (-90 for sprites that are facing up)
float angle = Mathf.Atan2(shootDirection.y, shootDirection.x) * Mathf.Rad2Deg - 90;
//Create a projectile instance
GameObject projectileInstance = Instantiate(projectile, position, Quaternion.AngleAxis(angle, Vector3.forward)) as GameObject;
//Add force to the objects rigidbody
projectileInstance.rigidbody2D.AddForce(shootDirection * shootSpeed, ForceMode2D.Force);
//Disable the weapon
disableWeaponFire();
//Set a timer to re-enable the weapon (effective for holding down the attack buttons)
Invoke("enableWeaponFire", shootDelay);
}
}
}