Create Gun thrower subclass of ratbot that spawns gun objects
This commit is contained in:
24
Assets/Scripts C#/Ratbots/GunThrower.cs
Normal file
24
Assets/Scripts C#/Ratbots/GunThrower.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts C#/Ratbots/GunThrower.cs.meta
Normal file
2
Assets/Scripts C#/Ratbots/GunThrower.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ebac0379db13f0c3932982bc0ebed28
|
||||
50
Assets/Scripts C#/Ratbots/Ratbot.cs
Normal file
50
Assets/Scripts C#/Ratbots/Ratbot.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Ratbot : MonoBehaviour
|
||||
{
|
||||
public float moveSpeed;
|
||||
public float damageStrength;
|
||||
public float damageTimeoutSeconds;
|
||||
public float health = 10;
|
||||
float lastDamageTime;
|
||||
|
||||
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() {
|
||||
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
|
||||
if (collision.gameObject.tag != "Player") return;
|
||||
if (lastDamageTime > Time.fixedTime-damageTimeoutSeconds) return;
|
||||
lastDamageTime = Time.fixedTime;
|
||||
collision.gameObject.GetComponent<Teo>().health -= damageStrength;
|
||||
}
|
||||
|
||||
// Die if health below zero
|
||||
public void Damage(float hurtyAmount) {
|
||||
Debug.Log(hurtyAmount);
|
||||
health -= hurtyAmount;
|
||||
if (health <= 0) {
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts C#/Ratbots/Ratbot.cs.meta
Normal file
2
Assets/Scripts C#/Ratbots/Ratbot.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d75aac518cf9dca0b16bb5a2e347204
|
||||
Reference in New Issue
Block a user