Basic Weapon Pickup in the RPG

Its not much yet, but being able to pick up weapons is super cool. How did you go with implementing a sword pickup?

I was almost certain you were going to miss the “Is Trigger” button, you did not…good job RIck, but then I remember all the great talk about the Unity Collision Matrix from the original course and sure enough you forgot the RB.

I would like to point out that no where in the course thus far have we had to add RB’s to our characters and as long as the Pickup has a RB it will still function the same…so for now I will leave the Character Prefabs without RB’s until it will be needed.

Quick Edit; I chose to click Is Kinematic on my RB so as not to affect physics within my game.

Thanks Rick and Sam, please continue.

1 Like

Yep, good point about where to place the rigidbody. I’ve made a quick edit in the video to reflect this.

1 Like

Also please note I was attempting to be funny in the first paragraph, a response to your initial question in the video about a mistake you made mid-way.

1 Like

Well, I just uploaded a video where the conversation went something along the lines of “can you guess what I did wrong this time… guess who forgot click is trigger?”
:slight_smile:

2 Likes

This was quite satisfying, after struggling a fair bit with the saving section, it’s good to get back to something I can take on as challenges!

Made a spinny sword function to the pick-ups to indicate they are pickups, and also added pressing [G] to drop whatever I’m holding. Thanks!

I am having an issue I cannot seem to figure out. I have not yet added two handed and see the github fighter class is different from mine. The issue is when I start the game is that I get a null reference to something in the Weapon.Spawn() method. I cannot attack until I equip the sword. My player and enemies have the default unarmed weapon on the prefab as well as the weapon SO’s have the needed prefabs and override. below is part of my fighter class. Not sure if I need to just continue on to video #113 and it will be covered if this is something I caused. Once sword is equipped no issues.

        private Health _target;
        Mover movement;
        [SerializeField]
        float timeBetweenAttacks = 1.0f;
        float timeSinceLastAttack = 0;
        [SerializeField] Weapon defaultWeapon = null;
        Weapon currentWeapon = null;
       [SerializeField] Transform handTransform = null;

        private void Awake()
        {
            movement = GetComponent<Mover>();
            EquiupWeapon(defaultWeapon);
        }

        public void EquiupWeapon(Weapon weapon)
        {
            currentWeapon = weapon;

            Animator animator = GetComponent<Animator>();
            weapon.Spawn(handTransform, animator);
            Debug.Log(handTransform + " transform");   
            
        }```

At a guess, Unarmed is trying to spawn a null gameobject. Make sure you have surrounded the code to spawn the weapon in Weapon.cs with an if(equippedPrefab!=null)

if (equippedPrefab != null)
            {
                Transform handTransform = GetHandTransform(rightHand, leftHand);

                GameObject weapon = Instantiate(equippedPrefab, handTransform);
                weapon.name = weaponName;
            }

I actually needed to add a capsule Collider and Rigidbody to the PlayerPrefab. Just having a Rigidbody wasnt enough to get the sword pickup to work. I also had to check the Is Kinematic checkbox otherwise movment got all messed up.

1 Like

i also had to check Is Kinematic or else the screen would shake if i gave to many movement clicks, is it because the terain is not full smooth?

That’s the physics system arguing with the NavMeshAgent. If you’re using a NavMeshAgent, you need to make the RigidBody isKinismatic.

got it, hey i have another question. You(brian_trotter) get compensation for you great service right?

Ask away, I’ll do the best I can. :slight_smile:

You are the best! but maybe you could shoot me your venmo or cash app, so i could show my gratitude?

Hello, I have got one small error with shaking the player when I tried to click anywhere and the enemy when they are dead, they seems flying…

A bit late probably but make sure you have your rigidbodies set to “Is Kinematic” this will ensure it doesn’t interfere with physics.