- Home /
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?
Answer by Casper 1 · Apr 24, 2011 at 09:29 AM
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)
Your answer
![](https://koobas.hobune.stream/wayback/20220613124831im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shooting from Bone and twisting via bone 0 Answers
Projectile motion of an arrow after applying force 3 Answers
GameObject look at velocity (not rigidbody) 0 Answers
How to fire an arrow. C# 2 Answers
In isometric gameplay , How to shoot arrow in projectile? 0 Answers