Start of top down shooter prototype, has a player that moves that can
shoot projectiles. Need projectile collision, and health. Need enemies. Need Kenny art.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
public InputAction movement;
|
||||
public InputAction mousePosition;
|
||||
public InputAction fireAction;
|
||||
public float speed = 10;
|
||||
public Camera mainCamera;
|
||||
public float fireRate;
|
||||
public GameObject projectileGo;
|
||||
public Transform projectileSpawn;
|
||||
|
||||
private Transform _transform;
|
||||
private Vector3 _position;
|
||||
private float _lastFireTime;
|
||||
private bool _wantFire;
|
||||
|
||||
private Action _aimingFunction;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
movement.Enable();
|
||||
// TODO: if no controller detected, enable mouse.
|
||||
mousePosition.Enable();
|
||||
_aimingFunction = MouseAim;
|
||||
// TODO: Aim stick
|
||||
|
||||
fireAction.Enable();
|
||||
fireAction.started += StartFire;
|
||||
fireAction.canceled += EndFire;
|
||||
_transform = transform;
|
||||
}
|
||||
|
||||
private void StartFire(InputAction.CallbackContext cbContext)
|
||||
{
|
||||
_wantFire = true;
|
||||
}
|
||||
|
||||
private void EndFire(InputAction.CallbackContext cbContext)
|
||||
{
|
||||
_wantFire = false;
|
||||
}
|
||||
|
||||
private void MouseAim()
|
||||
{
|
||||
var mousePos = mousePosition.ReadValue<Vector2>();
|
||||
var mouseWorld = mainCamera.ScreenToWorldPoint(mousePos);
|
||||
mouseWorld.z = _transform.position.z;
|
||||
var direction = mouseWorld - _transform.position;
|
||||
direction.Normalize();
|
||||
|
||||
var cross = Vector3.Cross(Vector3.up, direction);
|
||||
var dot = Vector3.Dot(Vector3.up, direction);
|
||||
var angle = Mathf.Acos(dot) * Mathf.Rad2Deg;
|
||||
|
||||
if (cross.z < 0)
|
||||
{
|
||||
angle *= -1;
|
||||
}
|
||||
|
||||
_transform.eulerAngles = Vector3.forward * angle;
|
||||
}
|
||||
|
||||
private void JoystickAim()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var direction = movement.ReadValue<Vector2>();
|
||||
direction.Normalize();
|
||||
direction *= speed * Time.deltaTime;
|
||||
|
||||
_position.x = direction.x;
|
||||
_position.y = direction.y;
|
||||
_position.z = 0;
|
||||
_transform.position += _position;
|
||||
|
||||
_aimingFunction();
|
||||
|
||||
if (_wantFire && _lastFireTime + (1 / fireRate) < Time.time)
|
||||
{
|
||||
_lastFireTime = Time.time;
|
||||
|
||||
Instantiate(projectileGo, projectileSpawn.position, projectileSpawn.rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 538fd7581e7114d4aa7467ae3532fcc2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public class Projectile : MonoBehaviour
|
||||
{
|
||||
private Transform _transform;
|
||||
private Rigidbody2D _rigidbody;
|
||||
|
||||
public float speed = 7;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_transform = transform;
|
||||
_rigidbody = GetComponent<Rigidbody2D>();
|
||||
_rigidbody.gravityScale = 0;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_transform.position += _transform.up * (speed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a91336669400a6f41b6160e63e0f6623
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user