Local vs. Global movement

I am instantiating a projectile from an object and giving it a forward global translation, but I would like to have it go in the forward direction locally. How would I accomplish this? And thank you.

You can use Transform.Translate() if you’re moving the object by directly manipulating its transform. If the object has a rigid body component attached, you can use AddRelativeForce(). Also, Transform.forward will give you the local forward direction vector for the object, which you can use to move the object directly (e.g. by modifying the ‘position’ property), or to create e.g. a force vector for use with Rigidbody.AddForce().

Thank you. do you think you could give me an example script though. when i tried to write my own it came up with errors. So far i have :
// Instantiate a rigidbody then set the velocity

var projectile : Rigidbody;

function Update () {
// left mouse was pressed, launch a projectile
if (Input.GetButtonDown(“Fire1”)) {
// Instantiate the projectile at the position and rotation of this transform

    var clone = Instantiate(projectile, transform.position, transform.rotation);
    clone.AddComponent("Rigidbody");
    // Give the cloned object an initial velocity along the current 
    // object's Z axis
    clone.velocity = transform.TransformDirection (Vector3.forward);
}

}
this sends the projectile in the forward direction Globally. if you could help make it local. also the console gives me this error when i press the mouse button to fire :
MissingFieldException: Field ‘UnityEngine.GameObject.velocity’ not found.
thank you for everything

transform.forward is the local “forward” direction (=direction of the +Z axis) of a transform. If that is not what you are looking for, please explain “go in the forward direction locally”.

i guess i am doing something wrong. it doesn’t matter what rotation the object that instantiates the projectile is at, the projectile still goes in the same direction, which is down in the top down style of my game. is that local? i am not too new to unity or scripting but i am no expert. thank you for your help

As long as you instantiate it in the direction you want it to travel, you could use something like:

transform.position += transform.forward * speed * Time.deltaTime;

to move it forward.

this code is in c#, but its an example of a projectile script I’m currently using in one of my projects. (I handle collision in the actor’s scripts and the layer is passed to the projectile based on the actor that fired it…

e.g. the player fires the bullet, the player script passes its layer to the targetLayer variable (a bit of a misnomer, but it woks), the target’s trigger then checks to see if the collision is in the enemy’s layer and that it is a bullet; I use the name bullet, but you could also give it the tag bullet)

public class BulletTarget : MonoBehaviour {

    public Vector3 source;
    public float range = 10.0F;
    public float bulletSpeed = 30.0F;
    public int targetLayer;



	
    void Start ()
    {
        gameObject.layer = targetLayer;
    }
	void FixedUpdate () {

        if (Vector3.Distance(source, transform.position) > range)
        {
            Destroy(gameObject);
        }
        
        transform.position += transform.forward * bulletSpeed * Time.deltaTime;
	
	}


    public float GetDamage()
    {
        return (Random.Range(10, 30));
    }
}