Why does my code shoot up but not forward?

I dont get why this is doing what it does my code is
using UnityEngine;
using System.Collections;

public class ProjectileController : MonoBehaviour {

	public float fireSpeed;
	public Rigidbody2D myRigidbody2D;
	public PlayerController player;

	public float fireLife;
	public float fireLifeStore;

	// Use this for initialization
	void Start () {
		myRigidbody2D = GetComponent<Rigidbody2D> ();
		player = FindObjectOfType<PlayerController> ();
	}
	
	// Update is called once per frame
	void Update () {
		GetComponent<Rigidbody2D>().AddForce(transform.forward * fireSpeed);
		//myRigidbody2D.velocity = new Vector2 (fireSpeed, myRigidbody2D.velocity.y);
		fireLife -= Time.deltaTime;

		if (fireLife <= 0) 
		{
			Destroy(gameObject);
		}
	}
}

Is the script attached to the projectile or a child?

Select the thing that it is attached to in the inspector. Make sure you use local for the gizmos and look where it is pointing at. Is it up or forward? (probably up because your script looks fine :wink: )

transform.forward is referring to the ProjectileController. I’m guessing ProjectileController is on an object that doesn’t move, I don’t know how you set this up. What if you use the player’s transform?

     GetComponent<Rigidbody2D>().AddForce( player.transform.forward * fireSpeed);

Hi @Treven,

Might I make the suggestion that you attach your projectile controller to your player character and then use scripting to instantiate/move your projectile from it’s desired location.

This would be a more common way of creating projectiles in game and probably easier to transfer onto new models/projects if you plan on future proofing your code.

You can get a good introduction to this concept in either of the following tutorials:

I’d be more than happy to look over your script in more detail if there’s anything your still not sure about and help anyway I can.

Ross