How to make projectile (spell) move from where the player is facing?

Hello everyone! I want to make so my spell moves from where the player is currently facing. Currently it only fires upwards…

public class LightningBolt : MonoBehaviour {

	private Transform myTransform;

	public int lightningSpeed = 5;

	// Use this for initialization
	void Start () {
		myTransform = transform;
	}
	
	// Update is called once per frame
	void Update () {
	
		// Lightning Bolt shooting up (for now)
		myTransform.Translate (Vector3.up * lightningSpeed * Time.deltaTime);

		// Destroy the object
		if (myTransform.position.y > 25) 
		{
			DestroyObject(gameObject);
		}
	}
}

Thank you in advance :slight_smile:

Instead of

   myTransform.Translate (Vector3.up * lightningSpeed * Time.deltaTime);

try:

   myTransform.Translate (transform.forward * lightningSpeed * Time.deltaTime);

This uses the forward vector on the gameObject’s transform instead of the world’s up vector.

Suppose you want the lightning bolt to come from you hand. What you do is you place an empty gameobject (you can name it spellOrigin or something) on your hand, facing away from you (the direction in which you wish to shoot). This game object should be parented to the hand so that when the hand moves, the gameobject moves. You would then instantiate your gameobject (lightning bolt) from the center of that empty gameobject (center or end, which ever looks best). Now, wherever you are facing, the lighning bolt will shoot directly away from you (towards the enemy you are targeting).

This code below should be attached to your empty game object (spellOrigin) that is attached to your players hand (assuming that is where you want the lightning bolt to originate)

You should have a lightning bolt prefab;

    using UnityEngine;
    using System.Collections;

    public class spellShoot : Monobehaviour{

    //Your lightning bolt prefab
    //added in the inspector
    public GameObject lightningBoltPrefab;

    //A currently public float, 
    //so you can adjust the speed as necessary.
    public float speed = 100f;

    void Update(){
            if(//whatever button you want to use the shoot is pressed){
                 //instantiate a lighning bolt and store it in 'theBolt'
                 GameObject theBolt = (GameObject)Instantiate(lightningBoltPrefab, transform.position, transform.rotation);

                //Add a force to the bolt going forward form your current position.
                theBolt.rigidbody.AddForce(gameobject.transform.forward * speed, ForceMode.Impulse);
            }
        }
    }