Coder Social home page Coder Social logo

llamacademy / scriptable-object-based-guns Goto Github PK

View Code? Open in Web Editor NEW
108.0 4.0 26.0 93.06 MB

Learn how to create a ScriptableObject-based gun system from scratch for your game!

Home Page: https://www.youtube.com/watch?v=E-vIMamyORg&list=PLllNmP7eq6TQJjgKJ6FKcNFfRREe_L6to

License: MIT License

C# 65.90% ShaderLab 29.36% HLSL 4.74%
game-development gamedev gamedevelopment gun scriptable-object scriptableobject tutorial unity-tutorial unity3d unity

scriptable-object-based-guns's Introduction

ScriptableObject-Based Gun System

Learn how to create a ScriptableObject-based gun system from scratch for your game!

In this tutorial repository and accompanying video series, you'll learn how to make a configuration-driven gun system with ScriptableObjects that will be able to have:

Impact Effects are not in scope of this tutorial series. They are handled using the Surface Manager (tutorial video here).

Youtube Tutorial

Quickstart

If you're following along with the tutorial series, check out the appropriate branch for the video you're watching. main will be the latest version and each branch such as part-1 will be where a particular video ended.

Make sure to import the Unity Particle Pack after checking out this repository for all the bullet shooting and impact effects. You can import just the folder "EffectExamples" and ignore the rest of the files. This will prevent overriding all your project settings.

Most likely you will also need to run the Render Pipeline Material Converter since we're using URP and Unity Particle Pack (1.0-1.7) ships with only Built-In Render Pipeline support. These particle systems loop, so you may also need to turn off "Looping" on each Particle System in use.

As of April 4, 2023, this uses Assembly Definitions (tutorial) to package the files. If you are using Assembly Definition files in your project, you may need to add references to LlamAcademy.Guns, LlamAcademy.ImpactSystem, and if you choose to use the demo scripts, LlamAcademy.Guns.Demo.

You will need to download "Rifle Idle" and "Pistol Idle" animations from Mixamo or your preferred animation for those states. The demo Animator assumes these exist already. You may need to hook them up on the "Gun Layer" of the StarterAssetsThirdPerson.controller

Scope of the Project

This repository is primarily intended as a learning tool with the Gun Series. However, you can absolutely take this and use it in your game. There are some key limitations to call out as this repository grows and we cover more things that if you're just pulling this repo, you should be aware of.

This repository is focused on Guns and making a flexible gun system to support all kinds of guns. How they work, configuring them, ammo types, this kind of thing. The core functionality of the Guns are housed in LlamAcademy.Guns and sub-namespaces. Anything listed as Demo was implemented to showcase some functionality, and may not be fully thought through. It can be used for reference implementation but may have holes in functionality and may not work "out of the box" for your game. This includes the Player Controller. A fully functional Player Controller is not (currently) in scope of this repository. I am using the Starter Assets Third Person Controller and doing some basic integrations to show a starting point only. Having different states such as "aiming" is more of a character controller function than strictly the gun system function.

Pull Requests

If there is something broken on a particular branch (each video has a dedicated branch that ends at the end state of that video), please feel free to open a PR to address that problem.

Since this is intended as a learning tool more than an "off the shelf solution for everything related to guns", requests can be made, but new features will not be added without an accompanying tutorial.

Aiming

Starting with Part 12, Aiming with the Animation Rigging package was added, and right click to aim in the ThirdPersonController was removed.

Aiming is handled by clamping the camera rotation to relatively closely match what the player's rig can bend to. It's important to note as well that ShootType.FromGun does not work well with Animation Rigging and IK since it has the Gun driving the aim target position, and the IK / Rigging trying to aim the gun. If you want to use "FromGun" the Animation Rigging should be disabled or weights set to 0.

Animations

Animations such as Pistol Idle and Rifle Aim cannot be included in the repository. You can find the Pistol Idle on Mixamo for free labeled "Pistol Idle". They also have a Rifle Idle animation you can bring in as well.

Supporters

Have you been getting value out of these tutorials? Do you believe in LlamAcademy's mission of helping everyone make their game dev dream become a reality? Consider becoming a Patreon supporter and get your name added to this list, as well as other cool perks. Head over to https://patreon.com/llamacademy to show your support.

If you'd prefer to become a YouTube Member you can get all the same benefits on that platform!

Want to provide one-time support? You can send a Super Thanks on any video!

Phenomenal Supporter Tier

  • YOUR NAME HERE!

Tremendous Supporter Tier

  • YOUR NAME HERE!

Awesome Supporter Tier

  • Ivan
  • Reulan
  • Iffy Obelus
  • Perry
  • Mustafa
  • Jerematic
  • YOUR NAME HERE!

Supporters

  • Trey Briggs
  • Matt Sponholz
  • Dr Bash
  • Tarik
  • Sean
  • Elijah Singer
  • Bruno Bozic
  • Ewald Schulte
  • Andrew Allbright
  • AudemKay x2
  • Claduiu Barsan-Pipu
  • Ben
  • Christiaan Van Steenwijk
  • Joseph Janosko
  • ChimeraDev
  • Wendy Whitner
  • Lukas Wolfe
  • Steve Martin
  • Jason Hansen
  • YOUR NAME HERE!

Other Projects

Interested in AI Topics in Unity, or other tutorials on Unity in general?

Requirements

  • Requires Unity 2021.3 LTS or higher.
  • You will need to download Rifle Idle and Pistol Idle animations from Mixamo or your preferred animation for those states.
  • Unity Particle Pack
  • Surface Manager - Included in this repository, but not covered in the tutorial series. Tutorial video here).

scriptable-object-based-guns's People

Contributors

llamacademy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

scriptable-object-based-guns's Issues

The bullet trajectory appears broken

When I was writing code following your first gunfight system tutorial, I found that after firing a few shots, the bullet trajectory started not at the muzzle, but in the direction of the last call to the trajectory. As shown in the figure:
微信图片_20240721202704
Later I thought that it might be because TrailRenderer did not clear the object data when it was recycled into the object pool. Then I added "instance.Clear();" to the PlayTrail method code in the GunScriptableObject class to solve the problem. The code is as follows (add code in the commented part):

        private IEnumerator PlayTrail(Vector3 startPoint, Vector3 endPoint, RaycastHit hit)
        {
            Debug.Log(startPoint+","+endPoint);
            TrailRenderer instance = _trailPool.Get();
            instance.gameObject.SetActive(true);
            instance.transform.position = startPoint;
            yield return null;
            instance.emitting = true;
            float distance = Vector3.Distance(startPoint, endPoint);
            float remainingDistance = distance;
            while (remainingDistance > 0)
            {
                instance.transform.position = Vector3.Lerp(startPoint, endPoint,
                    Mathf.Clamp01(1 - (remainingDistance / distance)));
                remainingDistance -= trailConfig.simulationSpeed * Time.deltaTime;
                yield return null;
            }

            instance.transform.position = endPoint;
            yield return new WaitForSeconds(trailConfig.duration);
            yield return null;
            // instance.Clear();
            instance.gameObject.SetActive(false);
            _trailPool.Release(instance);
        }

I want to know why you can still make the bullet trails not appear broken without clearing the TrailRenderer data。
I'm sorry for my poor English,Thanks for your tutorial,love from china.

Sample scene errors

Unity version: Mac silicon 2022.3.4f1
I downloaded the project as a zip file and started the project without doing any other things

NullReferenceException: Object reference not set to an instance of an object
LlamAcademy.Guns.GunScriptableObject.GetRaycastOrigin () (at Assets/Scripts/Guns/GunScriptableObject.cs:309)
LlamAcademy.Guns.Demo.PlayerAction.UpdateCrosshair () (at Assets/Scripts/Guns/Demo/PlayerAction.cs:46)
LlamAcademy.Guns.Demo.PlayerAction.Update () (at Assets/Scripts/Guns/Demo/PlayerAction.cs:41)

NullReferenceException: Object reference not set to an instance of an object
LlamAcademy.Guns.GunScriptableObject.TryToShoot () (at Assets/Scripts/Guns/GunScriptableObject.cs:190)
LlamAcademy.Guns.GunScriptableObject.Tick (System.Boolean WantsToShoot) (at Assets/Scripts/Guns/GunScriptableObject.cs:125)
LlamAcademy.Guns.Demo.PlayerAction.Update () (at Assets/Scripts/Guns/Demo/PlayerAction.cs:26)

NullReferenceException: Object reference not set to an instance of an object
LlamAcademy.Guns.Demo.Editors.PlayerActionEditor.OnSceneGUI () (at Assets/Scripts/Guns/Demo/Editor/PlayerActionEditor.cs:28)
(wrapper dynamic-method) System.Object.lambda_method(System.Runtime.CompilerServices.Closure,UnityEditor.Editor)
UnityEditor.SceneView.CallOnSceneGUI () (at /Users/bokken/build/output/unity/unity/Editor/Mono/SceneView/SceneView.cs:3782)
UnityEditor.SceneView.HandleSelectionAndOnSceneGUI () (at /Users/bokken/build/output/unity/unity/Editor/Mono/SceneView/SceneView.cs:2817)
UnityEditor.SceneView.DoOnGUI () (at /Users/bokken/build/output/unity/unity/Editor/Mono/SceneView/SceneView.cs:2485)
UnityEditor.SceneView.OnSceneGUI () (at /Users/bokken/build/output/unity/unity/Editor/Mono/SceneView/SceneView.cs:2325)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at /Users/bokken/build/output/unity/unity/ModuleOverrides/com.unity.ui/Core/IMGUIContainer.cs:355)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&) (at /Users/bokken/build/output/unity/unity/Modules/IMGUI/GUIUtility.cs:190)

There are other errors there, just they are not so critical.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.