- Home /
I've got a coding problem please help
im coding in java script and im trying to make my enemy shoot at the first person controller. is this the way to do it if it is then please help with my errors if it isnt please show me how.
var projectile : DragRigidbody;
var speed = 50;
var Player : Transform;
var Damage = 10;
var maxHealth = 100;
var curHealth = 100;
function Update ()
{
if(Input.OnTriggerEnter ("Fire1")) {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
Destroy (clone.gameObject, 10);
if (Player : Collider;
Damage - maxHealth = curHealth;
}
}
Answer by phxvyper · Nov 10, 2013 at 06:09 PM
The reason why it isn't working is because you destroy the game object right after you instantiate it.
Only destroy it when it collides with something (presumably the player).
e.g.
if (Input.OnTriggerEnter("Fire1")) {
close = Instantiate(projectile, transform.position, transform.rotation);
close.velocity = transform.TransformDirection(Vector3(0, 0, speed));
}
if (/*Put whatever collision detection flag you have here*/) {
Destroy(close.gameObject, 10);
curHealth -= Damage;
}
I do curHealth -= Damage;
instead of what you do in your code because Damage - maxHealth can return a negative, and if you ALWAYS subtract from the max health (unless the maxHealth isnt the Max Health, its actually the player's current health) then the players health will always be either maxHealth or Damage - maxHealth instead of it decreasing whenever the player is shot.
(e.g. take away from the player's current health in order to change it when the player gets shot)
He's actually waiting 10 seconds to destroy the instance.
Your answer
Follow this Question
Related Questions
Spline Controller (javascript) error 2 Answers
The name 'Player' does not denote a valid type ('not found'). 1 Answer
Basic enemy AI for 2.5D Platormer 1 Answer
I can't script enemy animation 0 Answers