I have a variable that checks if it is looking right or left (true = right, left = false), and I’ve tried the for hours to get it to “fire” the sprite ball in that direction after instantiating it, but it just doesn’t work. I’m at the point where I just dont know what to do anymore. Have anyone had this problem with 2d shooting? If so I would appreciate it if you could share what you did.
TL;DR, I’m trying to make a ball fire the correct Z direction. Thank you!!
Hi,
Why don’t you just do:
public Sprite FireSprite;
void Update () {
if (Input.GetKeyDown ("a")) {
Instantiate(FireSprite,transform.position,transform.rotation);}
}
Attatch that script to the player (shooter)
Create another script and attatch it to the FireSprite :
public int direction;
void Start () {
if (Player.DirectionFacing == "Right") {
direction = 1;
} else if (Player.DirectionFacing == "Left") {
direction = 2;
}
}
void Update () {
if (direction == 1) {
transform.Translate (0, 0, Time.deltaTime);
} else if (direction == 2) {
transform.Translate (0,0,-Time.deltaTime);
}
}
I didn’t test the code so i don’t know if it will work, let me know if it has any problem.
I hope i could help.
I have done like this: (this is in c#)
public Vector2 moving = new Vector2();
bool lookRight;
void Update()
{
if(Input.GetKey("d"))
{
moving.x = 1;
}
if(Input.GetKey("a"))
{ moving.x = -1;
}
if(moving.x > 0)
{
lookRight = true;
}else if(moving.x < 0)
lookRight = false;
}
void Fireball()
{
if(lookRight)
Rigidbody2D projectile = Instantiate(BubblePrefab, BubbleSpawn.position, BubbleSpawn.rotation) as Rigidbody2D;
projectile.AddForce(vector3.right * 500)
}else{
Rigidbody2D projectile = Instantiate(BubblePrefab, BubbleSpawn.position, BubbleSpawn.rotation) as Rigidbody2D;
projectile.AddForce(vector3.right * 500)
}
}
and then just call Fireball() where you have your keypress