- Home /
Bullet Script change direction with player (2D)?
Okay, I am having a little bit of an issue with a so far perfect scripting experience with unity. (Which I am very thankful for as I am learning more and more about programming in both C# and Javascript as I go). Needless to say, I am not very literate when it comes to coding. That isn't to say I don't understand syntax and the basics of these two languages, I'm just that good. LOL But that'll improve as I do more coding later on with the development of this game. Okay, anyways, I am trying to make it so that when my character sprite changes direction, the bullet also changes direction. This is a 2D platformer where the character only moves from left to right. I have tried implementing some of the solutions found on these forums into my code, but alas, it didn't fix the issue. To be specific with what I have trouble with understanding is the transform.position, and the transform.rotation of the Instantiate function. Also, the script that Instantiates the script is attached to an empty gameobject under the player if that makes a difference. Thanks in advance! Here is the code:
#pragma strict
function Start () {
}
function Update () {
if(Input.GetKeyDown(KeyCode.LeftShift))
{
Shoot ();
}
}
var bulletPrefab : GameObject;
function Shoot()
{
yield WaitForSeconds(0.2f);
Instantiate(bulletPrefab, transform.position, transform.rotation);
}
Answer by Sparkels · Aug 13, 2015 at 09:52 AM
Try this:
function Start () {
}
function Update () { if(Input.GetKeyDown(KeyCode.P)) {
Shoot();
}
}
function Shoot() { yield WaitForSeconds(0.2f);
this.gameObject.transform.position = GameObject.FindWithTag ("Fire").gameObject.transform.position;
Debug.Log("pow pow pow ");
}
Is this supposed to be the actual script or something to add to the script? I replaced it, and it didn't spawn the bullet prefab, and I added it, and it still didn't spawn the prefab even when I instantiated it. So, I really couldn't test if this code worked if my character was facing another direction. :/