- Home /
Objects Instantiating at wrong position
When I call:
function fireRocket(){
var rocketclone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation);
rocketclone.velocity = transform.TransformDirection (Vector3.forward * 300);
Destroy(rocketclone.gameObject,10);
}
It instantiates the rocket at position (0,0,0) with rotation (0,0,0), however it returns the position as that of the player (at whose position it is supposed to instantiate) when Debug.Log(rocketclone.position) is called.
Help!
FYI:
The script is attached to the player.
function fireRocket() is called from Update thus:
if (Input.GetButtonDown("Fire2")){
//shoot rocket;
fireRocket();
}
Entire Script:
//Inspector Variables
var maxSpeed : float = 80.0;
var minSpeed : float = 40.0;
var armor : float = 100.0;
var health : float = 100.0;
var curSpeed : float = 50.0;
var turnSpeed : float = 10.0;
var planecam : GameObject;
var bullet : Rigidbody;
var fireSpeed : float = 0.05;
var rocket : Rigidbody;
var ammoType : String;
var rocketAmmo : int = 8;
var controlNum : int = 360;
var damage : float;
//Private Variables
private var turn : float;
function Start(){
InvokeRepeating("fireGuns",0.0,fireSpeed);
}
function Update(){
turn = Input.GetAxis("Horizontal")*turnSpeed;
curSpeed = curSpeed + (Input.GetAxis("Speed")*0.2);
if(curSpeed<minSpeed){
curSpeed=minSpeed;
}
if(curSpeed>maxSpeed){
curSpeed=maxSpeed;
}
if (Input.GetButtonDown("Roll")){
//execute aileron roll function;
controlNum = 0;
}
if (Input.GetButtonDown("Fire2")){
//shoot rocket;
fireRocket();
}
if (Input.GetButtonDown("Special")){
//execute special function;
}
movePlane();
rollFunc();
if(damage!=0){
calcDmg();
}
}
function movePlane(){
if(controlNum>=360){
transform.Rotate(0,turn,0);
}
transform.Translate((Vector3(0,0,1)*curSpeed)/2,Space.Self);
planecam.transform.position.x = transform.position.x;
planecam.transform.position.z = transform.position.z;
}
function fireGuns(){
if (Input.GetButton("Fire1")){
var localOffset=Vector3(20,0,0);
var worldOffset1 = transform.rotation * localOffset;
var worldOffset2 = transform.rotation * -localOffset;
var cannon1 = transform.position + worldOffset1;
var cannon2 = transform.position + worldOffset2;
var clone : Rigidbody = Instantiate(bullet,cannon1,transform.rotation);
var clone2 : Rigidbody = Instantiate(bullet,cannon2,transform.rotation);
clone.velocity = transform.TransformDirection (Vector3.forward * (400 + (curSpeed*curSpeed)));
clone2.velocity = transform.TransformDirection (Vector3.forward * (400 + (curSpeed*curSpeed)));
Physics.IgnoreCollision(clone.collider, collider);
Physics.IgnoreCollision(clone2.collider, collider);
Destroy(clone.gameObject,5);
Destroy(clone2.gameObject,5);
}
}
function fireRocket(){
if(rocketAmmo>0){
var rocketclone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation);
rocketclone.velocity = transform.forward * 200;
var rscript : rocketScript = rocketclone.GetComponent("rocketScript");
rscript.type = ammoType;
Physics.IgnoreCollision(rocketclone.collider, collider);
rocketAmmo = rocketAmmo - 1;
}
}
function rollFunc(){
if(controlNum<=360){
collider.isTrigger = true;
transform.Rotate(0,0,5,Space.Self);
controlNum = controlNum + 5;
}
else{
collider.isTrigger = false;
}
}
function OnCollisionEnter(collision : Collision){
}
function calcDmg(){
var adam = damage/armor;
var hdam = damage - adam;
armor = armor - adam;
health = health - hdam;
damage = 0;
}
Just here to say I have the exact same problem, I think. The script with the instantiate function is attached to the player but the object instantiates where the player was spawned, not where they're currently at.
$$anonymous$$ine is unconnected to the spawn of the player. It instantiates at (0,0,0) wherever the player spawns.
Answer by botelho_gt · May 02, 2017 at 01:23 PM
Alright... long time later, but...
I have a script instantiating enemies e random positions, but i did something that was making my enemies being instantiated in wrong positions when i pressed play on the game. I found out that deactivating the Animator in the enemy inspector made the instantiation get correct again. Somehow its bugging the instantiation position...
Answer by Eric5h5 · Sep 09, 2012 at 03:47 AM
It's using the transform.position and transform.rotation of whatever object the script is attached to. If that's not what you want, you need to reference a different transform. (By the way, you can just do transform.forward instead of transform.TransformDirection (Vector3.forward). )
The script is attached to the player so transform.position / transform.rotation are that of the player (which is what I want), however it still appears at (0,0,0).
Thanks for the transform.forward thing!
I can't see anything that could be overriding it though. It's the only place I work on the rocket's transform.
It does:
//Inspector Variables var type : String;
//Private Variables
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "enemy"){ }
}
Answer by ArtOfWarfare · Oct 07, 2013 at 10:57 PM
Do you have an extra object in your scene that happens to also be spawning the rocket that shouldn't be?
I had the exact same symptoms as you and that ended up being my issue.