x


make bullet move forward.

I have this script `var bulletprefab:Transform;

function Update () {

if(Input.GetButton("Fire1"))
{
    var bullet = Instantiate(bulletprefab, GameObject.Find("SpawnPoint").transform.position, 
    Quaternion.identity);
    bullet.rigidbody.AddForce(Transform."2000");
}

}`

and I want the bullet to fire forward, but as it stands all it does is give me the error, Assets/scripts/bullet.js(9,42): BCE0023: No appropriate version of 'UnityEngine.Rigidbody.AddForce' for the argument list '(int)' was found.

I am new to unity and scripting so im a little confused, an explanation would be appritiated.

more ▼

asked Oct 26 '11 at 11:08 AM

Tomblade13 gravatar image

Tomblade13
1 2 2 2

Problem is, Transform."2000" isn't a thing. As in, not even slightly. Try using

bullet.rigidbody.AddForce(transform.forward * 2000);

instead. The rest of it looks pretty much ok, although you might want to put a few more explicit types in there (I don't know why javascript even has the other kind)

Oct 26 '11 at 10:24 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

About bullets and games there's a few things you should know.

If you're making a regular gunbullet, like, lets say from a pistol, then i wouldnt use a mesh/collider to detect if you've hit anything, there's 2 reasons for this.

  1. the bullet you spawn is an uneccesary drag on your systems resources, its moving too fast for anyone to see it anyway.

  2. This one is more important and is the main reason: If your bullet is moving above a certain velocity, depending on your frames per second, the collision will not be detected, this is because objects arent really moving when they go from point a to point b, they're teleporting small distances every frame, which in the end gives the illusion of movement. So, if the speed is too fast it will simply move through the object without detecting any collisions.

You can remedy this by using a Raycast, A raycast casts a ray in a given direction and sees if you hit anything, if it does it returns the object that was hit. this can be used instead of the bullet, or you can make the raycast originate from the bullet itself.

about your question, the line of code thats giving you the error is this one:

bullet.rigidbody.AddForce(Transform."2000");

you should write it as:

bullet.rigidbody.AddForce(new Vector3(0,2000,0)); 

instead, thats in c# btw, there should be something equivalent in javascript, here's a link to the documentation: Clicky

more ▼

answered Oct 26 '11 at 11:20 AM

Kacer gravatar image

Kacer
705 15 18 31

this help so much, apart from the bullets are now firing upwards instrad of accross. the game is a topdown game and the bullets are firing up. i dont suppose you know why this is?

Oct 26 '11 at 11:38 AM Tomblade13

thats because the "new Vector3" part of the code point up, a vector is a direction in 3D space, it looks like this: Vector3(x,y,z) so, if you put 2000 in the y spot it will push with a force of 2000 on the y-axis.

if you want it to go in another direction, try writing it like this: "new Vector3(2000,0,0)" or "new Vector3(0,0,2000)", you can use negative values as well.

Oct 26 '11 at 11:46 AM Kacer

I have found a major problem, the bullets fire in the same direction dispite the rotation of the character or spawnpoint.

Oct 26 '11 at 11:54 AM Tomblade13

try using vector3.forward

here's a link to what you can do with vector3 http://unity3d.com/support/documentation/ScriptReference/Vector3.html

Oct 26 '11 at 12:04 PM Kacer
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1862
x320
x254
x109

asked: Oct 26 '11 at 11:08 AM

Seen: 2764 times

Last Updated: May 27 at 04:09 PM