Files
gpd-924-en/SOLID/Assets/Scripts/NonSolid/PlayerController.cs
T
david a4f498a33d add unity project
add SOLID examples
2025-04-09 07:49:12 +02:00

73 lines
2.2 KiB
C#

using System;
using UnityEngine;
namespace DefaultNamespace.NonSolid
{
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
[SerializeField] private float _speed = 10.0f;
[SerializeField] private float _rotationSpeed = 60.0f;
[SerializeField] private Transform _shootSpawn;
[SerializeField] private Rigidbody _projectilePrefab;
[SerializeField] private float _shootForce = 10.0f;
[SerializeField] private FireType _fireType = FireType.Bullet;
private CharacterController _controller;
private Vector3 _movement = Vector3.zero;
private float _rotation = 0.0f;
private bool _isFiring = false;
private void Awake()
{
_controller = GetComponent<CharacterController>();
}
private void Update()
{
HandleInput();
Move();
Fire();
}
private void HandleInput()
{
_movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
_movement *= _speed * Time.deltaTime;
_rotation = Input.GetAxis("Mouse X") * _rotationSpeed * Time.deltaTime;
_isFiring = _isFiring || Input.GetButtonDown("Fire1");
}
private void Move()
{
_controller.Move(_movement);
_controller.transform.Rotate(Vector3.up, _rotation);
}
private void Fire()
{
if (!_isFiring) return;
_isFiring = false;
switch (_fireType)
{
case FireType.Bullet:
Rigidbody projectile = Instantiate(_projectilePrefab, _shootSpawn.position, _shootSpawn.rotation);
projectile.AddForce(_shootSpawn.forward * _shootForce, ForceMode.Impulse);
break;
case FireType.Spell:
break;
case FireType.Arrow:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}