- Home /
How to instantiate object at particular location
I have an object called "laucher" which instantiates "spears" every 4 seconds, the spears are supposed to go flying forwards from the launcher until they collide with a wall. however i'm having some problems understanding Instantiate.
This Script is Attached to "Launcher"
using UnityEngine;
using System.Collections;
public class Launcher : MonoBehaviour {
public GameObject Spear;
public float counter = 0;
void Update () {
if(counter <= 4)
{
counter += Time.deltaTime;
}
else{
Instantiate(Spear, new Vector2(-24,0), Quaternion.identity);
counter = 0;
}
}
}
How do I make the spear instantiate at the launcher itself?
How do I properly Instantiate the spear, because right now they're instantiated as Spear(Clone).
I can't just use a set location because I have multiple "Launcher" firing "Spear" at different spots of the level.
Answer by ahmedbenlakhdhar · Dec 11, 2014 at 01:32 AM
You can retrieve the current position of the game object containing the actual script by using
transform.position
.You are instantiating with the right way, you still can change the name of the created game object.
Try this:
// Instantiate at transform.position
GameObject newSpear = (GameObject)Instantiate(Spear, transform.position, Spear.transform.rotation);
// Change the name
newSpear.name = "Spear";
The spears that are currently being instantiated are invisible for some reason
Ohhh lol for some reason they were spawning in backwards, making them invisible because they're 2d
Is the game object which you are using to instantiate (Spear) is visible?
Spear is losing its prefab rotation upon instantiate for some reason. Should be 0,180,0 but it spawns in 0,0,0
By using this Quaternion.identity
you override the original rotation.
Use this ins$$anonymous$$d Spear.transform.rotation
. I updated the answer.
Your answer
Follow this Question
Related Questions
Dropping an object behind player in respect to rotation,How to drop an object behind the player 1 Answer
transform position and rotation of instantiated object 1 Answer
"Object reference not set to an instance of an Object" 0 Answers
How to make Camera position Independent of its Rotation? 1 Answer
Object instantiate, Projectile problems 2 Answers