Shoot directions

Hello every one ! (sorry for my bad english) i work with the unity 2d platformer project and i have actually this script for the gun : `using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour
{
public Rigidbody2D rocket; // Prefab of the rocket.
public float speed = 20f; // The speed the rocket will fire at.
private bool facingRight = true;

private PlayerControl playerCtrl;		// Reference to the PlayerControl script.
private Animator anim;					// Reference to the Animator component.

void Awake()
{
	// Setting up the references.
	anim = transform.root.gameObject.GetComponent<Animator>();
	playerCtrl = transform.root.GetComponent<PlayerControl>();
	Screen.showCursor = true;
}

void Update ()
{

	// If the fire button is pressed...
	if(Input.GetButtonDown("Fire1"))
	{
		// ... set the animator Shoot trigger parameter and play the audioclip.
		anim.SetTrigger("Shoot");
		audio.Play();

		// If the player is facing right...
		if(playerCtrl.facingRight)
		{
			// ... instantiate the rocket facing right and set it's velocity to the right. 
			Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(180f,0,0))) as Rigidbody2D;
			bulletInstance.velocity = new Vector2(speed, 0);
		}
		else
		{
			// Otherwise instantiate the rocket facing left and set it's velocity to the left.
			Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
			bulletInstance.velocity = new Vector2(-speed, 0);
		}
	}
}

}

I want the player shoot in every directions that the mouse point (360 degrees)… I have read a lot of topics about it but i don’t understand very well ! Is it someone who can help me with this please ?

So if I got it right, you want to click somewhere on the screen and shoot in the direction from the guy to the click position.

So what you first need is to convert your click from screen to raycast:

4th code example

Now the problem is that the raycast needs a collider to stop the ray so you should place an invisible wall, just a collider, right before the player position. So if your player is at z = 0 then place your wall at z = -0.1.

So the method will convert the position of the mouse to world position and throw a ray straight ahead until it hits the wall. The RaycastHit structure will have the position of the hit

hit.point;

now you want to get the direction between the player and that point. we can ignore the z in our case.

Vector3 direction = hit.point - player.transform.position;
direction.z = 0;

Now we have a vector from the player to the hit point. We need to normalize that vector since if we used the vector as is, we would get different result if the click is close or far away.

direction.Normalize();

this keeps the direction but makes the vector of length 1.

You now have all the data to instantiate and shoot your projectile. You don’t need to check where the player is facing since it is considered in the vector itself.

 Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(180f,0,0))) as Rigidbody2D;
 bulletInstance.velocity = new Vector2(direction.x * speed, direction.y*speed);

Why this isn’t work ? :confused: I have done what you said but it’s doesn’t work :confused:
using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour
{
	public Rigidbody2D rocket;				// Prefab of the rocket.
	public float speed = 20f;				// The speed the rocket will fire at.
	private bool facingRight = true;


	private PlayerControl playerCtrl;		// Reference to the PlayerControl script.
	private Animator anim;					// Reference to the Animator component.


	void Awake()
	{
		// Setting up the references.
		anim = transform.root.gameObject.GetComponent<Animator>();
		playerCtrl = transform.root.GetComponent<PlayerControl>();
	}




	void Update ()
	{

		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast(ray, 100))
			print("Hit something");
		hit.point;
		Vector3 direction = hit.point - player.transform.position;
		direction.z = 0;
		direction.Normalize();
		Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(180f,0,0))) as Rigidbody2D;
		bulletInstance.velocity = new Vector2(direction.x * speed, direction.y*speed);

		// If the fire button is pressed...
		if(Input.GetButtonDown("Fire1"))
		{
			// ... set the animator Shoot trigger parameter and play the audioclip.
			anim.SetTrigger("Shoot");
			audio.Play();



			// If the player is facing right...
			if(playerCtrl.facingRight)
			{

				// ... instantiate the rocket facing right and set it's velocity to the right. 
				Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
				bulletInstance.velocity = new Vector2(speed, 0);
			}
			else
			{
				// Otherwise instantiate the rocket facing left and set it's velocity to the left.
				Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
				bulletInstance.velocity = new Vector2(-speed, 0);
			}
		}
	}
}