- Home /
Firing arrows (FPS): few problems.
Hello! I recently started to play around with unity a bit and decided to make a shooting script for arrows. The problem is that the arrows start at the wrong rotation. The arrow is pointing towards the camera. (tried rotating the model i made in 3ds max then re import it to unity and rotating normally in unity).
The second bug i have is that when i look at over 45ish degree angle and shoot, the arrows hit something invisible (at least it looks like that) and their fly path is screwed up (using First Person Controller from Standard Assets).
Heres the shooting script:
var prefabBullet:Transform;
var shootForce:float;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
var instanceBullet = Instantiate(prefabBullet, transform.position, transform.rotation);
instanceBullet.rigidbody.AddForce(camera.transform.forward * shootForce);
}
}
Heres the arrow script:
var destroyTime : float;
function Start()
{
yield WaitForSeconds(destroyTime);
Destroy(gameObject);
}
function Update ()
{
transform.forward =
Vector3.Slerp(transform.forward, rigidbody.velocity.normalized, Time.deltaTime);
}
Answer by aldonaletto · Oct 06, 2012 at 11:20 PM
Imported models with wrong axes are more the rule than the exception - and the usual solution is to create an empty object (the arrow) and child the arrow model to it, aligning the model the way you want. The empty object is in fact the arrow, thus you should add a rigidbody and a long and thin box collider to it, as well as the arrow script. When the arrow object is ok, drag it to the project view to make it a prefab, and assign it to the prefabBullet variable in the Inspector.
Another hint: Destroy has an optional delay parameter, thus you don't need to use WaitForSeconds:
function Start(){
Destroy(gameObject, destroyTime);
}
Answer by Boog1eMan · Oct 11, 2012 at 06:53 PM
Thanx! That worked. It fixed both the rotation problem and the "hitting something ivnvisible" problem aswell, so now i can shoot the arrows at higher angles.
I cant jump and shoot at the same time, because the model spawns inside the player... I need to make it so that it has small offset. So that the arrow spawns in front of the player... Im currently using transform.position in my instantiate command. How would i add this offset to it?
The arrows try rotate after they're on the ground. Is there a way to turn the script off when a the arrows velocity drops below a certain point? or pehaps some better way to fix that?
Your answer
![](https://koobas.hobune.stream/wayback/20220613081942im_/https://answers.unity.com/themes/thub/images/avi.jpg)