- Home /
Question by
TheShadyColombian · Jul 03, 2014 at 03:50 AM ·
instantiatelocation
instantiate not creating in proper location
i have a script for an enemy that is supposed to make the enemy spawn "Poof" game objects when he dies (near the bottom) but it spawn them at 0,0 and slowly (slow enough that it gets destroyed (by another script) before it reaches the enemy) moves towards the enemy.
using UnityEngine;
using System.Collections;
public class ExperimentalEnemy : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public float maxDistance = 50f;
public int HitBoxRadius;
public ExperimentalDeath Damage;
public float Health = 10f;
private Transform myTransform;
public GameObject Char;
public Animator EnemyAnim;
public GameObject Poof;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
Char = GameObject.FindGameObjectWithTag("Player");
maxDistance = 40f;
HitBoxRadius = 4;
//Reference to DamageChar from the ExperimentalDeath.cs script
Component Damage = Char.GetComponent<ExperimentalDeath>();
}
// Update is called once per frame
void Update () {
if (Health < 0.1) {
EnemyDie ();
}
Debug.DrawLine(target.position, myTransform.position, Color.blue);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
if(Vector3.Distance(target.position, myTransform.position) < HitBoxRadius) {
Damage.DamageChar(Random.Range(0.01F, 0.2F));
Debug.Log ("Is Damaging " + Char);
}
}
public void EnemyDamage (float amount) {
Health -= amount;
}
public void EnemyDie () {
if(Random.Range(0f, 10f)< 1){
Instantiate (Poof, transform.position, transform.rotation);
}
Destroy (gameObject, 3);
EnemyAnim.SetBool ("Die", true);
}
void OnTriggerEnter (Collider other){
if (other.gameObject == GameObject.FindGameObjectWithTag ("Bullet")){
Destroy (other);
EnemyDamage(Random.Range(0.01F, 0.2F));
Debug.Log ("Bullet In Range");
Debug.Log ("Object in Range");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Terrain with lots of objects 1 Answer
cannon script is not working, no errors. 1 Answer
object count cap? 1 Answer
How to create a Graph in Unity 1 Answer