Mesh won't travel in direction it's facing

Hi,

I have an empty object instantiating ‘shrapnel’ gameobjects at a certain point in space.

This is a 2D game, so I only want objects to move in X and Y, not in Z.

When the pieces of shrapnel are instantiated, the gameObject has this script attached:

using UnityEngine;
using System.Collections;

public class shrapnel : MonoBehaviour {

float speed = 10;
Vector3 randomDirection;


// Use this for initialization
void Start () {
	
	float randomAngle = Random.Range (-135, 45);
	randomDirection = new Vector3(0, 0, randomAngle);
	
	transform.Rotate(randomDirection);
	
	transform.forward = transform.rotation * new Vector3(1,1,1);
	
	

}

// Update is called once per frame
void Update () {
	
	
	transform.Translate( transform.forward * speed * Time.deltaTime, Space.Self);

}

}

Now the best that I’ve been able to do is to get the object to rotate around its Z-axis, but then it just keeps travelling ‘into’ the screen (i.e. away from the orthographic camera) rather than downwards (which is what I’m aiming for).

The above script has my ‘bits of shrapnel’ firing at all angles, even though I’ve only asked for them to rotate in one direction.

So, ultimately, what I’m looking to do is when this object spawns it rotates to an angle within a range (pointing down in an angle) and then moves in that direction down the screen.

What have I missed here?

Many thanks in advance.

The following code should work.

using UnityEngine; 
using System.Collections;

public class shrapnel : MonoBehaviour 
{	
	float speed = 10;
	Vector3 randomDirection;
	
	// Use this for initialization
	void Start () 
	{		
		float randomAngle = Random.Range (-40, 40);	
		
		// Rotate object around z axis
		transform.Rotate(0f, 0f, randomAngle);				
	}
	
	void Update () 
	{	
		// (transform.up * -1) means objects down direction.
		transform.Translate( (transform.up * -1) * speed * Time.deltaTime, Space.Self);		
	}
}

You could also look at AddExplosionForce for an easier way of doing this with Physics.