- Home /
Bullet shoots up instead of forward?
Hello, I have this script attached to my gun model :
#pragma strict
var bulletPrefab : Transform;
var player : GameObject;
var force : float = 2000;
var shootForce : float = 1000;
function Update () {
if(Input.GetMouseButtonDown(0)){
//fire bullet
fire();
//audio clip
}
}
function fire(){
var instanceBullet = Instantiate(bulletPrefab,transform.position,Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward*shootForce);
}
But my problem is, The bullet shoots up instead of forward.
Help plz?
Click on "$$anonymous$$ain Camera" in the hierarchy and look at the inspector pane. What is its rotation? Is this a 2d or 3d game?
The you are working in world coords (in physics). $$anonymous$$aybe you have some object hierarchy in scene? Try AddForceRelative ins$$anonymous$$d. I am just guessing...
Answer by nbg_yalta · Jan 10, 2014 at 06:17 PM
Check your gun's local Z axis, this is transform.forward, I'm almost sure it goes up on your model... In this case your model facing directions can be x(transform.right) or y (transform.up)
Answer by helloiam · Jan 10, 2014 at 06:11 PM
#pragma strict
var bulletPrefab : Transform;
var player : GameObject;
var force : float = 2000;
var shootForce : float = 1000;
function Update () {
if(Input.GetMouseButtonDown(0)){
//fire bullet
fire();
//audio clip
}
}
function fire(){
var instanceBullet = Instantiate(bulletPrefab,transform.position,Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.right*shootForce);
}
Sometimes you have problems with the directions of your models in world space therefor sometimes Vector3.right, transform.right or up works.
If i use transform.up it goes backwards, and when i use transform.right it goes to the right.
But if i use Vector3.right it works but, If i rotate my character with gun and shoot it shooting another direction. (Sorry about my English, its not my $$anonymous$$ain Language!)
What you need to do is attach the children gameObject to your weapon because when you are moving the gameObject isn't moving with you.
Your answer
Follow this Question
Related Questions
bullets don't go forward 1 Answer
How can I shoot raycasts with slightly modified angles? 1 Answer
Adjusting transform (up, forward, whatnot) 1 Answer
Raycast target line of sight 1 Answer