54 lines
1.6 KiB
C#
Raw Normal View History

2020-03-11 21:11:27 +01:00
using UnityAtoms.BaseAtoms;
using UnityEngine;
2020-03-18 02:04:33 +01:00
namespace UnityAtoms.Examples
2020-03-11 21:11:27 +01:00
{
2020-03-20 01:29:39 +01:00
/// <summary>
/// Simple shooting scipt for the player using the arrow keys.
/// </summary>
2020-03-18 02:04:33 +01:00
public class PlayerShooting : MonoBehaviour
2020-03-11 21:11:27 +01:00
{
2020-03-18 02:04:33 +01:00
[SerializeField]
private GameObject _projectile;
2020-03-11 21:11:27 +01:00
2020-03-18 02:04:33 +01:00
[SerializeField]
private StringConstant _playerTag;
2020-03-11 21:11:27 +01:00
2020-03-18 02:04:33 +01:00
void Update()
2020-03-11 21:11:27 +01:00
{
2020-03-18 02:04:33 +01:00
var shootDirection = Vector3.zero;
var rot = Quaternion.identity;
if (Input.GetKeyDown(KeyCode.UpArrow))
{
shootDirection = Vector3.up;
rot = Quaternion.Euler(0f, 0f, 90f);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
shootDirection = Vector3.down;
rot = Quaternion.Euler(0f, 0f, -90f);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
shootDirection = Vector3.right;
rot = Quaternion.Euler(0f, 0f, 0f);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
shootDirection = Vector3.left;
rot = Quaternion.Euler(0f, 0f, 180f);
}
if (shootDirection != Vector3.zero)
{
var spawnPos = transform.position + shootDirection * 0.6f;
var projectile = Instantiate(_projectile, spawnPos, rot);
projectile.GetComponent<DecreaseHealth>().TagsAffected.Remove(_playerTag); // Turn off friendly fire
}
2020-03-11 21:11:27 +01:00
}
}
2020-03-18 02:04:33 +01:00
}