- Home /
Making a gun shoot WELL?
Hello, I've been using unity for a while, and I know basic scripting, although I'm still learning. I know that I'm going to get yelled at to go watch tutorials and whatnot, and to learn scripting, but I need a quick answer. I just want to complete the shooting and guns and core of the game, so I can focus on the story. All the tutorials I've watched make the bullet spawn at the wrong place, and they're all to complicated for such a simple thing. Below is the code for another Unity answer, but I get an error: Not a valid Identifier: 'nBullate'.
var bullet : GameObject;
var spawnPoint : Transform;
function Update () {
if(Input.GetKeyDown("mouse0")) {
var nBullet = Instantiate(bullet, spawnPoint.position, spawnPoint.rotation);
nBullate.rigidbody.velocity = spawnPoint.TransformDirection(Vector3(0,0,100));
}
}
Please help me. I understant everything in this script except for the nBullate part. Thanks in advance!
Answer by robertbu · Dec 14, 2013 at 01:09 AM
You've misspelled 'nBullet' on line 7...you have an extra 'a' in the name. Plus on line 6, you need to add 'as GameObject':
var nBullet = Instantiate(bullet, spawnPoint.position, spawnPoint.rotation) as GameObject;
Just wondering; I would tend to use
var nBullet:GameObject = Instantiate(bullet,spawnPoint.position,spawnPoint.rotation);
Is there any difference between the two?
@alienbaby - For Javascript I don't know. They don't have prefix casting. There is casting going on here because Instantiate() returns an Object, not a GameObject. For C#, see:
http://answers.unity3d.com/questions/27118/what-is-the-difference-between-prefix-casting-type.html
It didn't make my gun shoot. I assigned a bullet GameObject with a box collider and rigidbody. I also put an empty gameobject at the tip of my gun and parented it to the gun, then I dragged the empty gameobject to the transform. Please tell me if I did anything wrong. P.S. I assigned the script directly to my gun.
Also, don't reply that this isn't a forum and that I shouldn't reply.
Also, don't reply that this isn't a forum and that I shouldn't reply.
Not sure what this means. Providing comments when you tried to use a solution is just fine. What you should not (and have not done) is use an 'Answer' field to add comments.
As for your code, there is no key called 'mouse0'. Replace line 5 with:
if(Input.Get$$anonymous$$ouseButtonDown(0))