Unity Gaming: Shooting (part 1)

Okay so this is a little jump ahead of the Unity Gaming Infinite Runner Series. I’ve gotten a lot of questions about this so I’m writing about it now. Because it’s out of order, the examples are in a blank scene, not the Infinite Runner main scene.

Okay, so, most likely when you are shooting something it’s from either right from of the center of the screen or it’s, for the most part, originating from another objet like a hand or gun. This gets kinda complicated when all of a sudden you are using two cameras to judge the “center” of what you are looking at; like when using an oculus…

But no fear learning the general method of shooting isn’t too bad; and then adding the oculus bit will be easy to understand.

Raycasting

So when you aim for or align something in real life do you draw an invisible line from where you are to the thing you are trying to hit? Yes, Great! If not well, cool, but that’s what Raycasting is.
There are 5 possible parameters for the raycasting method in Unity.

  1. Vector3 origin – which is the position in 3d space where you want the ray to start
  2. Vector3 direction – which is the direction you want the ray to point, the first to parameters make a Ray.
  3. *RaycastHit hitInfo – get whatever the Ray hits first and store it in this parameter
  4. *Float maxDistance – this is the magnitude of the Ray aka how far out you want the ray to point
  5. *Int layerMask – what the Ray can Hit

* Indicates that these parameters are optional

There are also two main ways the Raycast method is written.

  • Raycast(Vector3 origin, Vector3 direction, float maxDistance, int layerMask)
  • Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask)

In Unity Raycast method return a Boolean value. True, if the ray hit something, and False if nothing was hit. I should also mention that in Unity Raycasts do not detect colliders for which the raycast is inside. However if you are creating an animation or moving, you should keep the Raycast method in a FixedUpdate method so that the physics library can update the data structures before the Raycast hits a collider at its new position.

Aiming

Now there are 2 types of aiming. One is simply shooting in the direction that the player is facing and one is shooting in the direction that the player is looking.
Let’s do the easy one first – aiming where the player is facing aka not aiming just shooting.
Cool so in my scene I have the regular First Person Character from the Unity Sample Assets package and created some ground by scaling a cube to have dimensions (10, 1, 30).

StartScene

Now lets create a shoot script called simpleShoot.

CreateSimpleScript

In the Update function we want to shoot a new sphere every time I click and shoot it in the “forward direction”. So let’s add the following lines.

void Update () {
    if (Input.GetButtonDown("Fire1"))
        {
            GameObject clonedBullet; 
            clonedBullet = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Sphere),
                transform.position, transform.rotation) as GameObject;
            clonedBullet.AddComponent<Rigidbody>();
            clonedBullet.GetComponent<Rigidbody>().AddForce(clonedBullet.transform.forward * 2000);
        }

    }

Cool and if you check it out I’m shooting and the bullets go in the forward direction of the transform of my character! YAY!

SimpleShootScene

Mouse Aim

Okay but that doesn’t shoot the ball in the upward direction when I’m looking up, you say to me. Yes I know that’s the next thing we are going to do. =) …in part two!

Happy Coding! Part 2 coming soon =D

-TheNappingKat


Leave a Reply

Your email address will not be published. Required fields are marked *