Shoot projectile from camera

var arrow:Transform;
var shootforce:float;

function Update () {

    if(Input.GetMouseButtonUp(0))
    {
        var instanceArrow = Instantiate(arrow, transform.position, transform.rotation);
        instanceArrow.rigidbody.AddForce(transform.forward * shootforce);
    }

}

This code shoots an arrow ahead of a first person controller, but it has some problems.

The arrows are backwards, the tip faces the character. It shoots it too low, it should be higher up. It shoots in the right direction, but if you look up it still shoots as if you're looking straight ahead. (This one may be more complicated, since it would need to rotate in the direction the arrow is facing while moving. You know how arrows work.)

Any tips?

1 Answer

1

Well what you probably want to get the mouse coordinates seeing that transform.position only returns the position of the camera, and not where the mouse is looking.

This is a similar question of yours: http://answers.unity3d.com/questions/7676/how-can-i-instantiate-a-gameobject-in-mouse-position-and-in-a-special-distance-fr

About the "shoots too low" just apply more force (Upwards) by:

instanceArrow.rigidbody.AddForce(transform.up * shootforce);

(Notice the transform.up)