- Home /
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.
Problem is, Transform."2000" isn't a thing. As in, not even slightly. Try using
bullet.rigidbody.AddForce(transform.forward * 2000);
ins$$anonymous$$d. 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)
Answer by Kacer · Oct 26, 2011 at 11:20 AM
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.
the bullet you spawn is an uneccesary drag on your systems resources, its moving too fast for anyone to see it anyway.
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
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?
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.
I have found a major problem, the bullets fire in the same direction dispite the rotation of the character or spawnpoint.
try using vector3.forward
here's a link to what you can do with vector3 http://unity3d.com/support/documentation/ScriptReference/Vector3.html
Answer by AstroneerKing · Jul 22, 2020 at 04:07 PM
You are sort of right that it is more performant to use Raycast, but a lot of games, for example, destiny II, use real bullet objects and it adds great visual effects to the game.
Your answer
![](https://koobas.hobune.stream/wayback/20220613022540im_/https://answers.unity.com/themes/thub/images/avi.jpg)