Cannot make bullet shoot right

Nomatter what I have tried for some reason the pullet refuses to shoot to the right or to the left. It is starting to get really annoying. All it keeps doing is shoot upwards and nothing more…

public GUITexture AtkText;

public Transform projectile;
public int speed = 20;
public bool cooldown = false;

	foreach (Touch touch in Input.touches)
	{
		if(AtkText.HitTest(touch.position) && touch.phase != TouchPhase.Ended && !cooldown)
		{
			Transform clone;
			var rot = projectile.rotation;
			projectile.rotation = rot * Quaternion.Euler(0, 0, 90);

			clone = (Transform)Instantiate(projectile, transform.position, projectile.rotation);
			cooldown = true;
			projectile.rigidbody2D.AddForce(projectile.transform.right * speed);
			clone.rigidbody2D.AddForce(clone.transform.right * speed);
		}
		else if(AtkText.HitTest(touch.position) && touch.phase == TouchPhase.Ended && cooldown)
		{
			cooldown = false;
		}
	}

2 Answers

2

if you are trying to make it shoot where you touch, why not find x and z coords of the touch point in world space, and instantiate the projectile, make it projectile.transform.lookAt(new Vector(x,0,y)) and add force to it in the forward direction.

Its not suppose to shoot at where you touch. The touch detections uses a GUI texture as a button to trigger the scripts so that the player shoots the bullet. Only directions it suppose to shoot throughout the whole thing is either Left or Right but it doesn't do either. Only shoots up.

wait are you working in 2d as in side view 2d? or top down 2d.

2d side view like a platformer kinda like Mario. And the bullet size at the moment is just a sphere for a temp texture till I get all mechanics working properly then the real development begins making the game run smoother and looking better. Shooting up is not a problem but I tend to not use it that way, so only left and right is more valuable.

OK apparently I was really stupid when it came to scripting this =.= but for future reference and for anyone else here is the fix:

			cooldown = true;
			if(facingRight)
			{
				Rigidbody2D bulletInstance = Instantiate(projectile, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
				bulletInstance.velocity = new Vector2(speed, 0);
			}
			else
			{
				Rigidbody2D bulletInstance = Instantiate(projectile, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
				bulletInstance.velocity = new Vector2(-speed, 0);
			}