Ranged attack: direction problem

I’m making a simple roguelike in Unity. I’ve a small problem, I’ve an attack called ‘Lightningbolt’ in the game. When I press the attack button, it fires as aspected. BUT, it only goes in one direction (upwards). I control the player with my mouse and I want the ligntning bolt to fire in the direction I’m controlling the ‘hero’. Say, if I’m going down with the hero, the attack shoots up. If I’m going up, it shoots down. Right, it shoots left and so on. Here’s the scripts:

Lightningbolt script (attached to the player):

private Transform myTransform;
	public float lightningSpeed = 5f;

	// 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);
		}
	}

Playermovement script:

if (Input.GetMouseButton (0))
		{
			Vector2 myPosition = new Vector2(this.transform.position.x, this.transform.position.y);
			Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
			Vector2 mousePosition2 = new Vector2(mousePosition.x, mousePosition.y);

			Vector2 target = mousePosition2 - myPosition;
			target.Normalize();
			target = target * Speed * Time.deltaTime/10;

			Vector2 newPos = myPosition + target;
			var allFloors = GameObject.FindGameObjectsWithTag("Floor");
			bool found = false;

			//Spells
			if(Input.GetKeyDown(KeyCode.Alpha1))
			{
				//Set the position for the spell
				LightningBolt.transform.position = myPosition;
//				//Fire the spell
				Instantiate(LightningBolt, transform.position, transform.rotation);
				audio.PlayOneShot(LightningBoltSound);
			}

Please help me…! :slight_smile:

just add a direction variable to your lightningbolt script and set it depending on what direction the player is moving in.

you already have the player’s move direction in your playermovement script.