MOD: Enemy AI
MOD: Player Shooting MOD: Damage Player and Enemy
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyMovement : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
44
ProjektUnity/Assets/Scripts/Enemy/EnemyMovementMelee.cs
Normal file
44
ProjektUnity/Assets/Scripts/Enemy/EnemyMovementMelee.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyMovementMelee : MonoBehaviour
|
||||
{
|
||||
public GameObject player;
|
||||
public EnemyStats enemyStats;
|
||||
public float targetDistance;
|
||||
|
||||
private float distance;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
enemyStats = GetComponent<EnemyStats>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if(player != null){
|
||||
// Calculate the distance between the enemy and the player
|
||||
distance = Vector2.Distance(transform.position, player.transform.position);
|
||||
|
||||
// Get the direction to the player by subtracting the enemy's position from the player's position
|
||||
Vector2 direction = player.transform.position - transform.position;
|
||||
direction.Normalize(); // Normalize the direction vector
|
||||
|
||||
// Calculate the angle to rotate the enemy to face the player
|
||||
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
||||
|
||||
// If the player is within a certain range, move towards them
|
||||
if (distance < targetDistance)
|
||||
{
|
||||
// Move the enemy towards the player's position
|
||||
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, enemyStats.speed * Time.deltaTime);
|
||||
|
||||
// Rotate the enemy to face the player
|
||||
transform.rotation = Quaternion.Euler(Vector3.forward * angle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58a007a46183a8947a29e53b7c347d41
|
||||
guid: 52630bacc6c11024e9cbe915465fc6d0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
@@ -8,16 +8,31 @@ public class EnemyStats : MonoBehaviour
|
||||
public Enemy enemy;
|
||||
public float health = 0;
|
||||
public float speed = 0;
|
||||
public float damage = 0;
|
||||
|
||||
private void Start() {
|
||||
health = enemy.currentHealth;
|
||||
speed = enemy.currentSpeed;
|
||||
health = enemy.getCurrentHealth();
|
||||
speed = enemy.getCurrentSpeed();
|
||||
damage = enemy.getDamage();
|
||||
}
|
||||
|
||||
//Für Später
|
||||
/* private void OnCollisionEnter2D(Collision2D other) {
|
||||
if (other.gameObject.CompareTag("Sword") || other.gameObject.CompareTag("Bullet")){
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
if (other.gameObject.CompareTag("Bullet")){
|
||||
BulletScript bulletScript = other.gameObject.GetComponent<BulletScript>();
|
||||
health -= bulletScript.weapon.getDamage();
|
||||
|
||||
if (health <= 0)
|
||||
{
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
|
||||
} else if(other.gameObject.CompareTag("Player")){
|
||||
speed = 0f;
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other) {
|
||||
speed = enemy.getCurrentSpeed();
|
||||
}
|
||||
}
|
||||
|
@@ -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:
|
@@ -5,14 +5,22 @@ using UnityEngine;
|
||||
[CreateAssetMenu(fileName = "Enemy", menuName = "Enemy/Enemy", order = 0)]
|
||||
public class Enemy : ScriptableObject
|
||||
{
|
||||
[HideInInspector]
|
||||
public float currentHealth = 0;
|
||||
|
||||
[SerializeField]
|
||||
private float health = 100;
|
||||
public float currentSpeed = 0;
|
||||
private float currentHealth = 100f;
|
||||
[SerializeField]
|
||||
private float currentSpeed = 2f;
|
||||
[SerializeField]
|
||||
private float damage = 20f;
|
||||
|
||||
private void Start() {
|
||||
currentHealth = health;
|
||||
public float getCurrentHealth(){
|
||||
return currentHealth;
|
||||
}
|
||||
|
||||
public float getCurrentSpeed(){
|
||||
return currentSpeed;
|
||||
}
|
||||
|
||||
public float getDamage(){
|
||||
return damage;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7b9b2eb7ebb420a48a5c3c12569223c8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,20 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "Player", menuName = "Player/Player", order = 0)]
|
||||
public class Player : ScriptableObject
|
||||
{
|
||||
[SerializeField]
|
||||
private float currentHealth = 100f;
|
||||
[SerializeField]
|
||||
private float currentSpeed = 5f;
|
||||
|
||||
public float getCurrentHealth(){
|
||||
return currentHealth;
|
||||
}
|
||||
|
||||
public float getCurrentSpeed(){
|
||||
return currentSpeed;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f29fe535863e5b41bcdfc7b87840f04
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02228afd5db5aef4eb69d87294994a8f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu(fileName = "Weapon", menuName = "Weapon/Weapon", order = 0)]
|
||||
public class Weapon : ScriptableObject
|
||||
{
|
||||
[SerializeField]
|
||||
private float damage = 20f;
|
||||
[SerializeField]
|
||||
private float fireRate = 0;
|
||||
|
||||
public float getDamage(){
|
||||
return damage;
|
||||
}
|
||||
|
||||
public float getFireRate(){
|
||||
return fireRate;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c14c23aaaf8bbfd4d8320990568224eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
ProjektUnity/Assets/Scripts/Weapon.meta
Normal file
8
ProjektUnity/Assets/Scripts/Weapon.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 63c15c1a3617b9047bea07d8b5b59d47
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
30
ProjektUnity/Assets/Scripts/Weapon/BulletScript.cs
Normal file
30
ProjektUnity/Assets/Scripts/Weapon/BulletScript.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BulletScript : MonoBehaviour
|
||||
{
|
||||
private Vector3 mousePos;
|
||||
private Camera mainCamera;
|
||||
private Rigidbody2D rb;
|
||||
public float force;
|
||||
|
||||
public Weapon weapon;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
mousePos = mainCamera.ScreenToWorldPoint(Input.mousePosition);
|
||||
Vector3 direction = mousePos - transform.position;
|
||||
Vector3 rotation = transform.position - mousePos;
|
||||
rb.velocity = new Vector2(direction.x, direction.y).normalized * force;
|
||||
|
||||
float rot = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg;
|
||||
transform.rotation = Quaternion.Euler(0, 0, rot + 90);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other) {
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
11
ProjektUnity/Assets/Scripts/Weapon/BulletScript.cs.meta
Normal file
11
ProjektUnity/Assets/Scripts/Weapon/BulletScript.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 622784c880831334db51422cb3845e4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user