- Home /
not instantiating at correct transform
i have made a script for a bullet, and when it hits a bird, it instantiates a feather particle system, except the particle system's position is miles away and i don't know why. here's the script: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { public float expireRate; private float currentTimer; public GameObject otherPlayer; public float movSpeed; public float damageRate; public GameObject feathers; //public Renderer rend; //public Collider coll; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { this.transform.Translate(Vector3.left * Time.deltaTime * movSpeed); currentTimer += 1 * Time.deltaTime; if (currentTimer > expireRate) Destroy(this.gameObject); } void OnCollisionEnter(Collision col) { if (col.gameObject.tag == "Bird") { //otherPlayer = col.gameObject; Debug.Log("killed-bird"); //Destroy(col.gameObject); //Destroy(gameObject); col.gameObject.GetComponent<Renderer>().enabled = false; //rend.enabled = false; //coll.enabled = false; Instantiate(feathers, col.transform); } Destroy(this.gameObject); } }
is there anyway to actually get it to instantiate at the right position? Thanks
Answer by Kristifor_p · Oct 13, 2019 at 08:19 PM
Here is my solution, i just wrote you a script you need to attach it to your bird, and tag your bullets with the tag "Bullet". Please take a look try it and if you have any problem with setting it up just let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BirdHit : MonoBehaviour
{
[Header("Bird Settings:")]
public GameObject featherParticle; // Put your particle prefab here.
public AudioClip birdDieSFX; // You could put some sound FX to be played when your bird die.
private bool isHit; // Bool that will control your paticle destruction.
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Bullet"))
{
Instantiate(featherParticle, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(birdDieSFX, transform.position);
Destroy(gameObject);
isHit = true;
Debug.Log("You hit the bird!");
// This will destroy the particle clone in hierarchy.
if(isHit)
{
Destroy(featherParticle, 1.0f);
}
}
}
}
Hope this will help you out @efobrog!
i didn't use all of it but some parts of the script fixed my problem. I also employed the death sounds :)
sorry i was late for reply, i got lazy with the game
Answer by WarmedxMints · Oct 07, 2019 at 12:51 PM
If you read the documentation here you will see that the Object.Instantiate method has a few overloads. You are setting its parent, not its position.
You need to pass a vector and rotation to set its position.
also, is there a way to not instantiate it to a child of the bird?
Your answer
Follow this Question
Related Questions
Help with moving instantiated object's transform 1 Answer
Moving a transform behaves odd 0 Answers
transform position and rotation of instantiated object 1 Answer
How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer
How can I get a GameObject's transform in game and set that to a variable? 1 Answer