using System.Collections;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class Weapon : MonoBehaviour
{
public bool singleFire = false;
public float fireRate = 0.1f;
public GameObject bulletPrefab;
public Transform firePoint;
public int bulletsPerMagazine = 30;
public float timeToReload = 1.5f;
public float weaponDamage = 15; //How much damage should this weapon deal
public AudioClip fireAudio;
public AudioClip reloadAudio;
[HideInInspector]
public WeaponManager manager;
float nextFireTime = 0;
bool canFire = true;
int bulletsPerMagazineDefault = 0;
AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
bulletsPerMagazineDefault = bulletsPerMagazine;
audioSource = GetComponent<AudioSource>();
audioSource.playOnAwake = false;
//Make sound 3D
audioSource.spatialBlend = 1f;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && singleFire)
{
Fire();
}
if (Input.GetMouseButton(0) && !singleFire)
{
Fire();
}
if (Input.GetKeyDown(KeyCode.R) && canFire)
{
StartCoroutine(Reload());
}
}
void Fire()
{
if (canFire)
{
if (Time.time > nextFireTime)
{
nextFireTime = Time.time + fireRate;
if (bulletsPerMagazine > 0)
{
//Point fire point at the current center of Camera
Vector3 firePointPointerPosition = manager.playerCamera.transform.position + manager.playerCamera.transform.forward * 100;
RaycastHit hit;
if (Physics.Raycast(manager.playerCamera.transform.position, manager.playerCamera.transform.forward, out hit, 100))
{
firePointPointerPosition = hit.point;
}
firePoint.LookAt(firePointPointerPosition);
//Fire
GameObject bulletObject = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Bullet bullet = bulletObject.GetComponent<Bullet>();
//Set bullet damage according to weapon damage value
bullet.SetDamage(weaponDamage);
bulletsPerMagazine--;
audioSource.clip = fireAudio;
audioSource.Play();
}
else
{
StartCoroutine(Reload());
}
}
}
}
IEnumerator Reload()
{
canFire = false;
audioSource.clip = reloadAudio;
audioSource.Play();
yield return new WaitForSeconds(timeToReload);
bulletsPerMagazine = bulletsPerMagazineDefault;
canFire = true;
}
//Called from WeaponManager
public void ActivateWeapon(bool activate)
{
StopAllCoroutines();
canFire = true;
gameObject.SetActive(activate);
}
}
using UnityEngine;
public class MovePlayer : MonoBehaviour
{
private CharacterController _controller;
private Vector3 _playerVelocity;
private Vector3 _moveDir;
private bool _groundedPlayer;
[SerializeField] private float _playerSpeed = 0.1f;
[SerializeField] private float _jumpHeight = 1.0f;
[SerializeField] private float _gravityValue = -9.81f;
private void Start()
{
_controller = gameObject.AddComponent<CharacterController>();
}
void Update()
{
_moveDir = _controller.transform.forward * Input.GetAxis("Vertical") + _controller.transform.right * Input.GetAxis("Horizontal");
_groundedPlayer = _controller.isGrounded;
if (Input.GetButtonDown("Jump") && _groundedPlayer)
{
_playerVelocity.y += Mathf.Sqrt(_jumpHeight * -3.0f * _gravityValue);
}
Gravity();
MovementLogic();
}
void MovementLogic()
{
_controller.Move(_moveDir * _playerSpeed * Time.deltaTime);
}
void Gravity()
{
if (_groundedPlayer && _playerVelocity.y < 0){
_playerVelocity.y = 0f;
}
_playerVelocity.y += _gravityValue * Time.deltaTime;
_controller.Move(_playerVelocity * Time.deltaTime);
}
}
using UnityEngine;
public class movePlayer : MonoBehaviour
{
private CharacterController _controller;
private Vector3 _playerVelocity;
private Vector3 _moveDir;
private bool _groundedPlayer;
[SerializeField] private float _playerSpeed = 2.0f;
[SerializeField] private float _jumpHeight = 1.0f;
[SerializeField] private float _gravityValue = -9.81f;
private void Start()
{
_controller = gameObject.AddComponent<CharacterController>();
}
void Update()
{
_moveDir = _controller.transform.forward * Input.GetAxis("Vertical") + _controller.transform.right * Input.GetAxis("Horizontal");
}
void FixedUpdate()
{
movementLogic();
}
void movementLogic()
{
_groundedPlayer = _controller.isGrounded;
if (_groundedPlayer && _playerVelocity.y < 0){
_playerVelocity.y = 0f;
}
_controller.Move(_moveDir * _playerSpeed);
if (Input.GetButtonDown("Jump") && _groundedPlayer)
{
_playerVelocity.y += Mathf.Sqrt(_jumpHeight * -3.0f * _gravityValue);
}
_playerVelocity.y += _gravityValue * Time.deltaTime;
_controller.Move(_playerVelocity * Time.deltaTime);
}
}
void Update()
{
_moveDir = _rb.transform.forward * Input.GetAxis("Vertical") + _rb.transform.right * Input.GetAxis("Horizontal");
}
private void MovementLogic()
{
_rb.AddForce(_moveDir*_moveSpeed, ForceMode.VelocityChange);
}
import IGridClikerAction as _grid
?