MOD: Enemy AI
MOD: Player Shooting MOD: Damage Player and Enemy
This commit is contained in:
@@ -12,12 +12,16 @@ public class PlayerMover : MonoBehaviour
|
||||
|
||||
|
||||
public Rigidbody2D rb;
|
||||
|
||||
public PlayerStats playerStats;
|
||||
public float speed = 2f;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
playerStats = GetComponent<PlayerStats>();
|
||||
speed = playerStats.speed;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
@@ -26,27 +30,26 @@ public class PlayerMover : MonoBehaviour
|
||||
Move();
|
||||
}
|
||||
|
||||
public void Move(){
|
||||
Vector2 moveDirection = Vector2.zero;
|
||||
if(Input.GetKey(up)){
|
||||
moveDirection += Vector2.up;
|
||||
}
|
||||
if(Input.GetKey(down)){
|
||||
moveDirection += Vector2.down;
|
||||
}
|
||||
if(Input.GetKey(left)){
|
||||
moveDirection += Vector2.left;
|
||||
}
|
||||
if(Input.GetKey(right)){
|
||||
moveDirection += Vector2.right;
|
||||
}
|
||||
|
||||
if(moveDirection.magnitude > 1){
|
||||
moveDirection.Normalize();
|
||||
}
|
||||
|
||||
rb.velocity = moveDirection * speed * Time.deltaTime * 1000;
|
||||
public void Move(){
|
||||
Vector2 moveDirection = Vector2.zero;
|
||||
|
||||
if(Input.GetKey(up)){
|
||||
moveDirection += Vector2.up;
|
||||
}
|
||||
if(Input.GetKey(down)){
|
||||
moveDirection += Vector2.down;
|
||||
}
|
||||
if(Input.GetKey(left)){
|
||||
moveDirection += Vector2.left;
|
||||
}
|
||||
if(Input.GetKey(right)){
|
||||
moveDirection += Vector2.right;
|
||||
}
|
||||
|
||||
|
||||
if(moveDirection.magnitude > 1){
|
||||
moveDirection.Normalize(); // Normalize if diagonal to avoid faster movement
|
||||
}
|
||||
|
||||
rb.velocity = moveDirection * speed;
|
||||
}
|
||||
}
|
||||
|
71
ProjektUnity/Assets/Scripts/Player/PlayerStats.cs
Normal file
71
ProjektUnity/Assets/Scripts/Player/PlayerStats.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerStats : MonoBehaviour
|
||||
{
|
||||
public Player player;
|
||||
public Weapon weapon;
|
||||
public float health = 0;
|
||||
public float speed = 0;
|
||||
public float damage = 0;
|
||||
public float fireRate = 0;
|
||||
|
||||
private bool isTakingDamage = false;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
health = player.getCurrentHealth();
|
||||
speed = player.getCurrentSpeed();
|
||||
damage = weapon.getDamage();
|
||||
fireRate = weapon.getFireRate();
|
||||
}
|
||||
|
||||
private void OnCollisionEnter2D(Collision2D other)
|
||||
{
|
||||
if (other.gameObject.CompareTag("Melee"))
|
||||
{
|
||||
if (!isTakingDamage)
|
||||
{
|
||||
StartCoroutine(TakeDamageOverTime(other.gameObject.GetComponent<EnemyStats>()));
|
||||
}
|
||||
|
||||
if (health <= 0)
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (other.gameObject.CompareTag("Bullet") && !isTakingDamage){
|
||||
BulletScript bulletScript = other.gameObject.GetComponent<BulletScript>();
|
||||
health -= bulletScript.weapon.getDamage();
|
||||
|
||||
if (health <= 0)
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCollisionExit2D(Collision2D other)
|
||||
{
|
||||
if (other.gameObject.CompareTag("Melee"))
|
||||
{
|
||||
isTakingDamage = false;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator TakeDamageOverTime(EnemyStats enemyStats)
|
||||
{
|
||||
isTakingDamage = true;
|
||||
while (isTakingDamage && enemyStats != null)
|
||||
{
|
||||
health -= enemyStats.damage;
|
||||
|
||||
yield return new WaitForSeconds(1f);
|
||||
}
|
||||
}
|
||||
}
|
11
ProjektUnity/Assets/Scripts/Player/PlayerStats.cs.meta
Normal file
11
ProjektUnity/Assets/Scripts/Player/PlayerStats.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b920576e7fca6774cac105ba69e65302
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
48
ProjektUnity/Assets/Scripts/Player/Shooting.cs
Normal file
48
ProjektUnity/Assets/Scripts/Player/Shooting.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Shooting : MonoBehaviour
|
||||
{
|
||||
private Camera mainCam;
|
||||
private Vector3 mousePos;
|
||||
|
||||
public GameObject bulletPref;
|
||||
public Transform bulletTransform;
|
||||
public bool canFire;
|
||||
private float timer;
|
||||
|
||||
public PlayerStats playerStats;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
|
||||
playerStats = gameObject.GetComponentInParent<PlayerStats>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);
|
||||
|
||||
Vector3 rotation = mousePos - transform.position;
|
||||
|
||||
float rotZ = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg;
|
||||
|
||||
transform.rotation = Quaternion.Euler(0, 0, rotZ);
|
||||
|
||||
if(!canFire){
|
||||
timer += Time.deltaTime;
|
||||
if(timer > playerStats.fireRate){
|
||||
canFire = true;
|
||||
timer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(Input.GetMouseButton(0) && canFire){
|
||||
canFire = false;
|
||||
Instantiate(bulletPref, bulletTransform.position, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
}
|
11
ProjektUnity/Assets/Scripts/Player/Shooting.cs.meta
Normal file
11
ProjektUnity/Assets/Scripts/Player/Shooting.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d46013563bc18b149b0cc625199050ae
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user