2D Firing an object in the direction the Player is facing!

Hi I am wondering how to get an object to fire in the direction the player is facing. I am currently firing an arrow from a bow object which is attached to the player object in order to maintain movement and to rotate the placeholder sprite with the player.

I have attached my bow script below. I know that my code in the Bow is what is causing the arrow to fire in a straight line but I am unsure on how to make it fire in the same direction the player is facing.

// BOW SCRIPT

public class Bow : MonoBehaviour {

	public Rigidbody2D arrow;				// Prefab of the arrow.
	public float projSpeed = 20f;			// The speed of the projectile
	public Vector3 Rotation;				// Vector 3 to hold rotation
	private PlayerMovement playerMove;		// Reference to the PlayerControl script.
	//private PlayerRotation playerRot;		// Reference to the Player Rotation Script
	
	void Awake()
	{
		// Setting up the references required
		// playerMove = transform.root.GetComponent<PlayerMovement>();
	}

	// Update is called once per frame
	void Update () 
	{
		// Check to see if Fire 1 is pressed
		if(Input.GetButtonDown("Fire1"))
		{
			// Instantiate an arrow!
			Rigidbody2D arrowInstance = Instantiate(arrow, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))) as Rigidbody2D;
			arrowInstance.velocity = new Vector2(projSpeed, 0);
		}
	}

}

Thanks in advance.

solved. you need to check "Trigger" on terrain only, uncheck the player.

2 Answers

2

Assuming the object the script avove is attached to is facing the correct direction to fire the arrow, you can change line 25 to:

arrowInstance.velocity = transform.forward * projSpeed;

Hi, This has helped shoot it in the direction that the player is facing! Thank you so much! I ended up copying the players rotation to the bow object (which is attached to the player object) via code. Is there a way to do it so the bow will inherit the direction from the player without the use of code? (sorry for all the questions, I am a unity noob).

Not sure without more info. Typically the spawn point would be a child of the player. Assuming the child has a rotation of (0,0,0), then the child's rotation and the parent's rotation will match. But getting the rotation from another game object is perfectly okay.

Using arrowInstance.velocity = transform.up * projSpeed; got this code working properly for me. My 2D game has bullets firing out onto the Z plane.

using UnityEngine;
using System.Collections;

public class DanteControllerScript : MonoBehaviour 
{
	public float maxSpeed = 10f;
	bool facingRight = true;
				
	public Rigidbody2D bullet;
	
	Animator anim;

	// Use this for initialization
	void Start () 
	{
		anim = GetComponent<Animator> ();
	}

	// Fixedupdate: do all physic controls here
	void FixedUpdate () 
	{
		float hMove = Input.GetAxis ("Horizontal");
		float vMove = Input.GetAxis ("Vertical");

		anim.SetFloat ("hSpeed", Mathf.Abs (hMove));
		anim.SetFloat ("vSpeed", vMove);

		rigidbody2D.velocity = new Vector2 (hMove * maxSpeed, rigidbody2D.velocity.y);
		rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x, vMove * maxSpeed);


		if (hMove > 0 && !facingRight)
			Flip ();
		else if (hMove < 0 && facingRight)
			Flip ();

		if (Input.GetButtonDown ("Fire1")) {
			Rigidbody2D bulletInstance = Instantiate (bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))) as Rigidbody2D;
			bulletInstance.velocity = transform.forward * maxSpeed;
		}
	}

	void Flip()
	{
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}
}

I am having the same issue.

Anyone knows what the problem might be? My bullet shoots down all the time instead of the players facing direction.

Thanks