Create Gun thrower subclass of ratbot that spawns gun objects

This commit is contained in:
2024-10-27 17:59:13 -07:00
parent 77f507183d
commit 89953e7cd6
18 changed files with 587 additions and 10 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 80a6f9e1f8fdfdbcfb3c74283bd49306
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,24 @@
using UnityEngine;
public class GunThrower : Ratbot
{
public GameObject gun;
public float throwSpeed;
void Start() {
SetVars();
InvokeRepeating(
"ThrowGun",
Random.Range(1, 3),
Random.Range(1, 3)
);
}
void ThrowGun() {
GameObject thrown = Instantiate(gun);
thrown.GetComponent<Rigidbody2D>().AddRelativeForce((
transform.position - player.transform.position
).normalized * throwSpeed);
thrown.GetComponent<Gun>().damageStrength = damageStrength;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3ebac0379db13f0c3932982bc0ebed28

View File

@@ -7,21 +7,29 @@ public class Ratbot : MonoBehaviour
public float damageTimeoutSeconds;
public float health = 10;
float lastDamageTime;
GameState gameState;
GameObject player;
public static GameState gameState;
public static GameObject player;
void Start() {
SetVars();
}
public void SetVars() {
// Set gameState and player objects
player = GameObject.FindGameObjectWithTag("Player");
gameState = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameState>();
}
}
void Update() {
// Constantly move towards player
MoveTowardsPlayer();
}
public void MoveTowardsPlayer() {
Rigidbody2D controller = GetComponent<Rigidbody2D>();
Vector2 direction = (player.transform.position - transform.position).normalized;
controller.AddRelativeForce(direction*moveSpeed*Time.deltaTime);
}
}
void OnCollisionStay2D(Collision2D collision) {
// Deal damage when colliding with the player on a 0.5 second cooldown