Making the enemy shoot in the direction the player is facing.

TITLE CORRECTION: Make the projectile the player shoots travel in the direction the player is facing.

I have a 3rd person spaceship that I can fly on a 3D space and make it look at the mouse cursor, so it’s not necessarily facing the direction in which it is moving (like in Geometry Wars).

What I want now is for the ship to shoot in the direction it is facing. At the moment I only know how to use the Vector3.up or Vector3.forward, but these don’t take into account the direction the ship is facing…

So inside the projectile script where I tell it the direction of travel, how can I tell it to travel in the direction the ship was facing the moment it was spawned?

Here is my current projectile script (C#):

using UnityEngine;
using System.Collections;

public class PlayerProjectile : MonoBehaviour {
	
	public float ProjectileSpeed;
	//public GameObject ExplosionPrefab;
	
	private Transform myTransform;
	
	// Use this for initialization
	void Start () 
	{
		myTransform = transform;
	}
	
	// Update is called once per frame
	void Update () 
	{
		float amtToMove = ProjectileSpeed * Time.deltaTime;
		myTransform.Translate(Vector3.forward * amtToMove);
	}
}

Try setting the orientation of the projectile to be the same as the orientation of the spaceship when you first fire it.

I know that’s what I should do, I just don’t know how. :slight_smile:

You don’t need to copy ‘myTransform = transform’ - just use transform directly. Assuming when you create the projectile you point it in the right direction, all you need to do is:

    transform.Translate(transform.forward * amtToMove);

If the object’s transform isn’t aligned to the motion (so the bullet is not travelling directly forwards) then store a vector saying which way it is travelling when you create it, and add that on here instead of using transform.forward.

I’m not seeing how you are creating the projectiles, I’d imagine when you are doing that you would be setting the initial position of the projectile to be based on the position of the spaceship. If so you would then just need to do something like

projectile.transform.rotation = spaceship.transform.rotation;

in the same code i.e. when you have a reference to both.

	void Update () {
					if (Input.GetButtonDown("Fire1"))
		{
			// fire projectile
			Vector3 position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
			Instantiate(ProjectilePrefab, position, Quaternion.identity);
		}
	}

This is what firing the projectile - attached to the player object.

How do I do that? How do I capture the current rotation of the ship and tell the projectile to travel down its Z-axis?

In your code to create the projectile, where you currently pass Quaternion.identity, instead pass transform.rotation. Then it will copy the ship’s orientation.

you could change

Instantiate(ProjectilePrefab, position, Quaternion.identity);

to

Instantiate(ProjectilePrefab, transform.position, transform.rotation);
 Vector3 position = new Vector3(transform.position.x, transform.position.y, transform.position.z);

is not needed

YES! Thank you very much kind sir! It works.

I have this problem with my enemy that on an uneven Terrain he moves fine but shoots horizontally. I used ray to see me.When I am getting in the ray direction my health is decreasing but the bullets move horizontally

here is the script

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
public Transform target;
public float movespeed;
private float playerDistance;
public float lookDistance;
public float attackDistance;
public float enemydamageamounttoplayer = 20f;
public GameObject enemybullet;
public float count;
public float delaytime=1;

// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{
Vector3 direction = target.position - this.transform.position;
playerDistance = Vector3.Distance (target.position, this.transform.position);

if ( playerDistance< lookDistance)
{

direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,Quaternion.LookRotation(direction),0.1f);
}

if(playerDistance<attackDistance)
{
Vector3 pos = transform.position;

float x = transform.localScale.y;

transform.position = new Vector3 (pos.x, Terrain.activeTerrain.SampleHeight (pos) + (x), pos.z);

transform.Translate (0, 0, 1f * movespeed * Time.deltaTime);

attack();

}

}
public void attack()
{
if (count > delaytime)
{
RaycastHit hit;

Ray ray;

ray = new Ray (transform.position, target.position - this.transform.position);

Instantiate (enemybullet,transform.position, transform.rotation);

if (Physics.Raycast (ray, out hit, 100f))
{

if (hit.transform.tag == “Player”)
{
hit.transform.GetComponent ().RemovePlayerHealth (enemydamageamounttoplayer);
}
}
count = 0;
}
count += Time.deltaTime;
}

}