Firing projectile in the Z direction.

I am trying to make a top down game and I have a projectile firing from the player however I cant seem to get it to work correctly. It works only some of the time… If I move around and shoot it sometimes goes different directions. Here is my Scipt:

using UnityEngine;
using System.Collections;

public class BouncyBall : MonoBehaviour {

    public Rigidbody BouncyBallPrefab;
    private Transform ballTransform;
    public float Speed;

    void Start()
    {        
        ballTransform = transform;
    }
	
	// Update is called once per frame
	void Update () 
    {
        if (Input.GetKeyDown("left ctrl"))
        {
            Rigidbody instantiatedBouncyBallPrefab = (Rigidbody)Instantiate(BouncyBallPrefab, transform.position, transform.rotation);
            instantiatedBouncyBallPrefab.velocity = new Vector3(transform.position.x, transform.position.y, Speed) ;

        }
	}
}

I’m guessing that the player and the projectile collides when you shoot.

Either instantiate the projectile a bit away from the player, or use IgnoreCollisions to make the projectile go through the player.

When you’re setting the velocity of the ball, why are you using the transform coordinates for the x and y components? For a projectile’s velocity, if you only want it moving forward, you would only need to set the z component. x and y values are going to interfere with that.

both valid replies. I believe my problem is that i am colliding with the projectile… i will try that…

Should I be using quantian.identity then? Not sure how I would proved the vector 3 with anything else…

instantiatedBouncyBallPrefab.velocity = new Vector3(transform.position.x, transform.position.y, Speed) ;

It should be just like this line;

instantiatedBouncyBallPrefab.velocity.z = Speed;

You were both right… Thanks for the help guys! Also moving my central ground location from center at 0 to top left corner at 0, 0 helped also.

Tried that before… gives me a conversion error… this works:

instantiatedBouncyBallPrefab.velocity = transform.TransformDirection(0, 0, transform.position.z * Speed);