- Home /
The question is answered, right answer was accepted
How am I supposed to use GetComponent?
I hate to ask this question especially when it has been asked so many times, but I just don't get how get component works. All the other threads I've been to haven't helped in the slightest.
What I am trying to do is make an a fireball deal damage to another object when it collides with it. Usually I would use sendmessage, but here the player can charge up their fireball and make it do increased damage.
SO what I want to do is have the fireball directly take away the object's health based on it's charge level (if the object has a health variable).
using UnityEngine;
using System.Collections;
public class FireBall_beh : MonoBehaviour
{
// Gameplay stuff
public static float chargeLevel = 100f;
public float speed = 12f;
public float turnRate = 3f;
public float lifeTime = 3f;
private GameObject target;
private Vector3 rayDir;
// Charge level stuff
private float particleStartSpeed;
private float particleEmissionRate;
private float particleRadius;
public float baseDamage = 15f;
void OnEnable ()
{
// change size depending on the charge level
particleStartSpeed = particleSystem.startSpeed;
particleEmissionRate = particleSystem.emissionRate;
particleRadius = transform.localScale.y;
particleSystem.startSpeed = (particleStartSpeed / 100) * chargeLevel;
particleSystem.emissionRate = (particleEmissionRate / 100) * chargeLevel;
transform.localScale = transform.localScale = new Vector3((particleRadius / 100) * chargeLevel, (particleRadius / 100) * chargeLevel, (particleRadius / 100) * chargeLevel);
// Die after an ammount of time
Invoke("Die", lifeTime);
// Do a sphere cast to see if there are players to lock onto
RaycastHit hit;
rayDir = transform.forward;
if(Physics.SphereCast(transform.position, 10, rayDir, out hit, 20f))
{
//Debug.Log(hit.collider.name);
if(hit.collider.tag == "Player")
{
target = hit.collider.gameObject;
}
}
}
void FixedUpdate ()
{
// Move forward
transform.Translate(0,0, speed * Time.deltaTime);
// If this object has a target
if(target != null)
{
// Check if the target is infront
Vector3 targetDir = target.transform.position - transform.position;
float angle = Vector3.Angle(targetDir, transform.forward);
// Follow the target
if(angle >= -90f && angle <= 90f )
{
Vector3 lookTarget = target.transform.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(lookTarget);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnRate);
}
}
}
void OnTriggerEnter(Collider other)
{
// Calculate damage
float damage = (baseDamage / 100) * chargeLevel;
// Get the script of the other object and take away it's health (if it has a health variable)
// Die
Die();
}
void Die()
{
// Change particel system paramaters back to normal
particleSystem.startSpeed = particleStartSpeed;
particleSystem.emissionRate = particleEmissionRate;
transform.localScale = new Vector3(particleRadius, particleRadius, particleRadius);
// Clear target
target = null;
// Deactivate
gameObject.SetActive(false);
}
}
The chargeLevel variable is a percentage 100 being how it is normally and it increases to 300% damage and size (this variable is set in the player script when the player fires it)
Thanks for all and any help and if there is a better way to do this please let me know.
Answer by MartinTV · Feb 09, 2014 at 03:40 AM
I think I have just come up with a much simpler way to do this, that was kind of obvious, so don't pay attention to this hahah.