Simple Shooting Script

Made a simple script, I understand it all. But could someone also explain Vector2,3,4 to me also? I didn’t quite understand that well, but I do know what it does.

Anyways, main issue is I try to do some_var.rigidbody.AddForce(Vector3.up*shootForce); to make it shoot forward, but it won’t work. Could anyone lend me a hand please? Thanks, Code at the bottom.

function Start () {

	var shootForce: float;
	var bulletPrefab: Transform;
	var myMuzzle: Transform;

function Update () {
    	var bullet = Instantiate(bulletPrefab,myMuzzle.transform.position,Quaternion.identity);
   	 	bullet.rigidbody.AddForce(transform.forward * shootForce * Time.deltaTime);


Vector2, 3 and 4 are structures in Unity used to represent and contain float values within each vector. You can look at them as limited float arrays. Vector2 (X,Y) are normally used for 2D representations, this would for instance apply to textures or mouse movement. Vector3 (X, Y, Z) are normally used for representing a point in 3d. Vector4 (X, Y, Z, W) are mostly used when working with shaders and the float values of colors (RGBA).


What your code currently does is to instantiate a GameObject prefab, then give the rigidbody on the instantiated GameObject a force in the forward direction of the transform the script is attached to. The problem I can see is that the force is too small for you to even notice any change to the rigidbody. As this only happens once to each bullet you would want to add an impulse force immediately with its mass calculated. Then make sure that the variable shootForce is ok in the Inspector, start on a low number (10 - 100) to make sure that you see the bullet.

    bullet.rigidbody.AddForce(transform.forward * shootForce, ForceMode.Impulse);

As you don’t add force to the same object over several frames you won’t need to regard the delta time. Remember that if you want to add force over several frames you need to be inside FixedUpdate which is the physics time step.

Thanks so much you helped me so much for my game