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.
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
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.