- Home /
Create GameObject with another gameObject's position
Hello,
I'm new to unity, and I'm trying to make something that looks like Space invaders. When the player presses the spacebar, I generate a cube, and then translate it updwards. What I'd like to do is, when I create the cube, which would be the bullet, create it on the same position as the player.
Here's my code:
bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.AddComponent ('translateBullet');
and the translateBullet.js has:
function Start(){
var player = GameObject.FindWithTag('Player');
transform.position = player.transform.position;
transform.position = Vector3.up * 5;
transform.localScale.x = 20;
transform.localScale.y = 20;
transform.localScale.z = .1;
}
function Update () {
transform.Translate(Vector3.up * 1.5);
}
Answer by croucho · Mar 29, 2012 at 04:30 PM
you're overwriting the position of your transform.
you first set it to the player's position but then set it to the up vector times 5 (completely blowing away it's old position).
Try this:
transform.position = player.transform.position + ( Vector3.up * 5 );
OR
transform.position = player.transform.position; transform.position += ( Vector3.up * 5 ); // note the += not just =
Thanks alot mate, it worked like a charm! I had an error before, but for some reason the player hadn't been tagged so. I thought it would set it to the players positions, and then do Vector3.up. ty :D
Your answer
Follow this Question
Related Questions
translate 2 Answers
Transform.Position Collision Issue 0 Answers
Tranlation doesn't affect position 0 Answers