- Home /
My bullet wont move forward
So my script wont work...
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
function Start () {
}
function Update () {
if(Input.GetButtonDown("Fire1")) {
Fire();
}
}
function Fire(){
var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet1.AddForce(transform.forward*BulletSpeed);
}
the bullet is created but wont move!
Edit :
This image shows the Inspector settings : imgur.com/a/Ct16P
[1]: http://www.imgur.com/a/Ct16P
Answer by clunk47 · Dec 12, 2012 at 04:36 AM
You didn't have your instantiation defined correctly. The new "bullet1" needs to be set as a "clone" pretty much. You just had var bullet1 : Rigidbody; then your instantiate command for Bullet. It needs to be var bullet1 = Instantiate(Bullet... Because when you were adding force to bullet1, it was null. It's also a good idea to disable gravity on this rigidbody since it's going to be used as a bullet. As well, set the collision detection to Continuous Dynamic if you want it to be able to detect such fast collisions. I just used mousebutton 0 (left click) here so I could test it ( I don't have Fire defined.) You actually may have had the first part set up right, the format on this site is making it show up wrong. But if you try this script, it will work.
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var fwd : Vector3;
function Start ()
{
}
function Update ()
{
fwd = transform.TransformDirection(Vector3.forward);
if(Input.GetMouseButtonDown(0))
{
Fire();
}
}
function Fire()
{
var bullet1 : Rigidbody;
bullet1 = Instantiate(Bullet, Spawn.position, Spawn.rotation) as Rigidbody;
bullet1.rigidbody.useGravity = false;
bullet1.rigidbody.AddForce(fwd * BulletSpeed, ForceMode.Impulse);
}
IS there any way i can upvote you or somthing? but thank you so much for the help! it worked
Just some tips on using this 'site (for ALL new users) :
How to reply to an answer / post a comment :
To make a comment , USE the [add new comment] button, a window then opens to type in. The answer fields are for ANSWERS, so unless you are answering your own question , DON'T write in an answer box. This helps the 'site work properly, especially when other people are searching for answers, and want to read answers , not comments.
How to accept an answer :
On the left-hand-side of the Answer box , there are the following icons :
Thumb Up
Number (of votes)
Thumb Down
A Tick/check mark
If an answer worked for you , click on the 'Tick' , the answer should now be highlighted in green. If you like an answer on Any question , you can click on the Thumb UP , the thumb should now be highlighted in green , and the number of votes should rise by 1.
When your karma rises above 15, you can upvote any answer (on any question) you like or think is correct, this gives the person karma for their time and effort.
IF your question changes slightly while commenting and reading comments , EDIT the original Question, so anyone reading from the beginning knows what you are asking.
This will make for a happy experience for everyone. I made mistakes starting on this 'site too, but everyone is helpful if you learn and change these habits.
Following these simple steps helps the website work , and other readers to find answers also.
Happy Coding =]
the FAQ appears at the bottom of the page : http://answers.unity3d.com/page/faq.html
Your answer
Follow this Question
Related Questions
how can i add a gun sound to this script? 0 Answers
Script in UNITY ??? 2 Answers
Error in script 1 Answer
i need some code suggestions 1 Answer
Gun Script Error (Fixed!!!) 1 Answer