using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace StarterAssets
{
public class StarterAssetsInputs : MonoBehaviour
{
[Header("Character Input Values")]
public Vector2 move;
public Vector2 look;
public bool jump;
public bool sprint;
[Header("Attack Inputs")]
public bool attack1;
public bool attack2;
public bool attack3;
public bool attack4;
public bool attack5;
public bool attack6;
[Header("Movement Settings")]
public bool analogMovement;
[Header("Mouse Cursor Settings")]
public bool cursorLocked = true;
public bool cursorInputForLook = true;
#if ENABLE_INPUT_SYSTEM
public void OnAttack1(InputValue value) => attack1 = value.isPressed;
public void OnAttack2(InputValue value) => attack2 = value.isPressed;
public void OnAttack3(InputValue value) => attack3 = value.isPressed;
public void OnAttack4(InputValue value) => attack4 = value.isPressed;
public void OnAttack5(InputValue value) => attack5 = value.isPressed;
public void OnAttack6(InputValue value) => attack6 = value.isPressed;
public void OnMove(InputValue value)
{
MoveInput(value.Get<Vector2>());
}
public void OnLook(InputValue value)
{
if(cursorInputForLook)
{
LookInput(value.Get<Vector2>());
}
}
public void OnJump(InputValue value)
{
JumpInput(value.isPressed);
}
public void OnSprint(InputValue value)
{
SprintInput(value.isPressed);
}
#endif
public void MoveInput(Vector2 newMoveDirection)
{
move = newMoveDirection;
}
public void LookInput(Vector2 newLookDirection)
{
look = newLookDirection;
}
public void JumpInput(bool newJumpState)
{
jump = newJumpState;
}
public void SprintInput(bool newSprintState)
{
sprint = newSprintState;
}
private void OnApplicationFocus(bool hasFocus)
{
SetCursorState(cursorLocked);
}
private void SetCursorState(bool newState)
{
Cursor.lockState = newState ? CursorLockMode.Locked : CursorLockMode.None;
}
}
}
using StarterAssets;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Windows;
public class PlayerAttackNewInput : MonoBehaviour
{
public Animator animator;
public float meleeCooldown = 0.5f;
public float rangedCooldown = 1f;
[Header("Melee Settings")]
public float[] meleeDamages = { 10f, 15f, 20f };
public Transform meleeHitPoint;
public float meleeHitRange = 1f;
public LayerMask enemyLayer;
[Header("Ranged Settings")]
public GameObject[] projectilePrefabs; // index 0 = attack4, 1 = attack5, 2 = attack6
public Transform projectileSpawnPoint;
public float projectileSpeed = 10f;
private float lastAttackTime;
public StarterAssetsInputs input; // assign in inspector
void Update()
{
if (input.attack1)
{
MeleeAttack(0);
input.attack1 = false;
}
if (input.attack2)
{
MeleeAttack(1);
input.attack2 = false;
}
if (input.attack3)
{
MeleeAttack(2);
input.attack3 = false;
}
if (input.attack4)
{
RangedAttack(0);
input.attack4 = false;
}
if (input.attack5)
{
RangedAttack(1);
input.attack5 = false;
}
if (input.attack6)
{
RangedAttack(2);
input.attack6 = false;
}
}
void MeleeAttack(int index)
{
if (Time.time - lastAttackTime < meleeCooldown) return;
lastAttackTime = Time.time;
animator?.SetTrigger($"Melee{index + 1}");
Debug.Log("melee Attack" + index);
Collider[] hitEnemies = Physics.OverlapSphere(meleeHitPoint.position, meleeHitRange, enemyLayer);
foreach (Collider enemy in hitEnemies)
{
enemy.GetComponent<EnemyHealth>()?.TakeDamage(meleeDamages[index]);
}
}
void RangedAttack(int index)
{
if (Time.time - lastAttackTime < rangedCooldown) return;
lastAttackTime = Time.time;
animator?.SetTrigger($"Ranged{index + 1}");
Debug.Log("range Attack" + index);
if (projectilePrefabs[index] != null)
{
GameObject projectile = Instantiate(projectilePrefabs[index], projectileSpawnPoint.position, projectileSpawnPoint.rotation);
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.linearVelocity = projectileSpawnPoint.forward * projectileSpeed;
}
}
void OnDrawGizmosSelected()
{
if (meleeHitPoint != null)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(meleeHitPoint.position, meleeHitRange);
}
}
}
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float maxHealth = 50f;
private float currentHealth;
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(float amount)
{
currentHealth -= amount;
Debug.Log(gameObject.name + " took " + amount + " damage. HP left: " + currentHealth);
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log(gameObject.name + " died!");
// Optional: play death animation
// animator.SetTrigger("Die");
Destroy(gameObject); // remove enemy from scene
}
}