Shooting with Unity Starter Assets - First Person Character Controller

Hi.

I had a little problem implementing shoot, because I’m working with the Unity Starter Assets - First Person Character Controller, which has the new input system. Fortunately, I found a video of CodeMonkey, where he explains how to shoot using this asset pack and I thought I should share it, so anyone with the same problem would have the solution: Awesome Third Person Shooter Controller! (Unity Tutorial) - YouTube.

In short:

1, In StarterAssets (Input Action Asset) make a new input action for Firing (you can use Jump as an example)

2, Using the example of jump/sprint, add the following lines to StarterAssetsInputs.cs:

public bool fire;

public void OnFire(InputValue value)
{
    FireInput(value.isPressed);
}

public void FireInput(bool newFireState)
{
    fire = newFireState;
}

3, In Weapon.cs add the following lines:

private StarterAssetsInputs starterAssetsInputs;

void Awake() 
{
    starterAssetsInputs = FindObjectOfType<StarterAssetsInputs>();
}

4, Lastly, I added the line starterAssetsInputs.fire = false; at the end of my Shoot(); method, this way I will only Shoot once when pressing the left mouse button:

void Shoot()
{
    RaycastHit hit;
    Physics.Raycast(FPCamera.transform.position, FPCamera.transform.forward, out hit, range);
    Debug.Log("I hit" + hit.transform.name);
    starterAssetsInputs.fire = false;
}

I hope this will be of some help! :slight_smile:

6 Likes

Thank you so much. This helped a lot!

1 Like

Thanks, I was doing the same and got stuck for a moment after adding the inputs.
Could not remember how to call them in Weapon.cs. Obviously FindObjectOfType(); is the surefire way to go.

1 Like
  1. You need to add “using StarterAssets;” inside Weapons.cs.

  2. If you preffer to make auto shooting possible while button is pressed, then set it similiar to Sprint, instead of Jump.

You could set cooldown between each fire (similiar to JumpTimeout from FirstPersonController.cs) , so you could shoot for example 10 times per second, or 200. You don’t have to set fireInput to false after each Shoot() in this scenario :wink:

private void Shoot()
    {
        RaycastHit hit;
        Physics.Raycast(FPCamera.transform.position, FPCamera.transform.forward, out hit, range);
        if(hit.collider)
        {
            Debug.Log("hit this thing: " + hit.transform.name);
        }
    }
1 Like

One thing that makes the whole thing work that I haven’t seen mentioned… In update, you need to check that the input has been called and call the Shoot(); method.

    void Update(){
        if (starterAssetsInputs.fire){
            Shoot();
        }

Hello there, i will try do add to the variety of this post: If you’re not using the starter assets, but maybe your own controller and are using the input system, this could maybe help?

This uses the action-asset workflow with a C# wrapper. Scroll down to the bottom half of the manual.

Implementation
using UnityEngine;
using UnityEngine.InputSystem;

public class Weapon : MonoBehaviour
{
    PlayerControls playerControls;
    [SerializeField] Camera playerCamera;
    [SerializeField] float range;

    private void Awake()
    {
        playerControls = new PlayerControls();
    }

    void OnClick(InputAction.CallbackContext context)
    {
        Debug.Log("Click");
        Shoot();
    }
    void Shoot()
    {
        RaycastHit hit;
        Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, range);
        Debug.Log(hit.transform.name);
    }

    private void OnEnable() { playerControls.MoveLook.Click.Enable(); playerControls.MoveLook.Click.performed += OnClick; }
    private void OnDisable() { playerControls.MoveLook.Click.Disable(); playerControls.MoveLook.Click.performed -= OnClick; }
}

Hi, first of all - HUGE thank you for that post! It helps a lot! :slight_smile: I have a question - I am doing ( I think :stuck_out_tongue_winking_eye: ) everything as you mentioned, however I am getting:

“Assets\Scripts\Weapon.cs(13,13): error CS0246: The type or namespace name ‘StarterAssetsInputs’ could not be found (are you missing a using directive or an assembly reference?)”

not sure why, any thoughts what might be the reason?

My weapon script:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Weapon : MonoBehaviour

{

    [SerializeField] Camera FPCamera;

    [SerializeField] float range = 100f;

    // [GM] added to be able to shoot

    private StarterAssetsInputs starterAssetsInputs;

    void Awake()

    {

        starterAssetsInputs = FindObjectOfType<StarterAssetsInputs>();

    }

    // [GM] end

   

    void Update()

    {

        if (starterAssetsInputs.fire)

        {

            Shoot();

        }

    }

    private void Shoot()

    {

        RaycastHit hit;

        Physics.Raycast(FPCamera.transform.position, FPCamera.transform.forward, out hit, range);

        Debug.Log("I hit" + hit.transform.name);

        starterAssetsInputs.fire = false;  // [GM] added

    }

   

}

and as you can see StarterAssetsInputs.cs exists…:
image

Hi!

I’m glad that I could help! You need to add using StarterAssets; at the top and I think it will solve your problem :slight_smile:

1 Like

ahhhh :woman_facepalming: as a reference to the namespace… yeah that make lot of sense! being a being a beginner is confusing :stuck_out_tongue: … Thank you for super fast response! <3 Have an AWESOME day!

1 Like

Privacy & Terms