- Home /
I cant get my 2d shooting script to work?
Been trying to get a 2d shooting script to work the past few hours and it's frustrating now. Could anyone point out what I need to change? First time I work with 2d in Unity. Thanks in advance!
This is attached to the Player. It just spawns infront of it without doing anything. Any ideas? Thanks again.
#pragma strict
var BubblePrefab : Rigidbody;
var BubbleSpawn : Transform;
var spawnDistance : float = 10.0f;
function Start () {
}
function Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
GameObject.Instantiate(BubblePrefab, BubbleSpawn.position, BubbleSpawn.rotation);
transform.position += Time.deltaTime * 5 * transform.forward;
}
}
What do you actually want it to do? It appears if you hit the Spacebar once you will Instantiate a bubble prefab and move your players transform? Are you looking to move the bubble forward, like shooting?
I think transform.forward is the 'z' axis and in 2D it will look like it doesn't go anywhere. Try Vector3.up
Answer by RedDevil · Aug 12, 2014 at 12:15 PM
var GA : GameObject = Instantiate(BubblePrefab, BubbleSpawn.position, BubbleSpawn.rotation) as GameObject;
GA.transform.position += Time.deltaTime * 5 * transform.forward;
im usualy using C# so i dunno if you need as gameobject at the end of the first line in JavaScript
Answer by JonnyHilly · Oct 15, 2014 at 08:16 PM
you should add a script to your bubble prefab, that moves it forwards by itself. move this line.... into that scripts update function
transform.position += Time.deltaTime 5 transform.forward;
also get rid of the 5 and make it a variable, so you can easily tweak the speed.
Now whenever you hit space... it will spawn a new bubble... and the bubble will move itself forwards. and you can spawn as many bubbles as you like, and they will all take care of themselves.