- Home /
Object not inheriting transform from parent
I'm making a Space Invaders clone as a learning exercise (Unity beginner here).
When I press fire I want a shot to appear at the player's position of course (I haven't got to making the shot travel yet). At the moment when I press fire the 'playerShot' appears in the centre of the screen, not where the player is.
What am I doing wrong? The script is attached to the player object and so it should inherit the default position and rotation of the player shouldn't it?
The only idea I had was maybe it's an issue because I'm using Orthello2D? I was thinking of making my objects sprites rather than rigid bodies because I thought that would be more efficient. Am I wrong?
Code below. Any help much appreciated.
using UnityEngine; using System.Collections;
public class player : MonoBehaviour {
//Figure used to change speed of left/right movement
public float moveSpeed = 400.0f;
public GameObject playerShot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Setting variables and defining basic left/right player movement
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
transform.Translate(h, 0, 0);
//Fire mechanism
if (Input.GetButtonUp("Fire1")){
OTSprite newPlayerShot = Instantiate (playerShot, transform.position, transform.rotation) as OTSprite;
}
}
}
Answer by sparkzbarca · May 14, 2013 at 11:35 AM
well you did say 2d.
a position and a rotation are 3 dimensional and i'm not sure how OTSprites work.
You need to look at the OTSprite documentation to see an example of how a sprite is spawned. Perhaps it uses vector2?
You could try spawning as a gameobject and then static casting to a sprite?
Thanks for helping.
Yeah, I'll delve into the Orthello docs to see what that gives. It's not as good as Unity's docs though :-( .
I also wasn't sure whether I should just spawn it as a rigidbody anyway. Is that bad practice?
Sorry for flood of noob questions - but what do you mean 'static casting' to a sprite?
Thanks again.