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!

1 Like

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.

Despite my scripts matching Rick’s, and reviewing to make sure I’ve set all the related game objects up correctly, my player still isn’t picking up the sword.

Here is my character prefab’s rigidbody (kinematic is set to true):

Here is the Sword Pickup (Is Trigger is set to true):
Screenshot 2021-11-05 101638

and here is the whole WeaponPickup script:

I’ve made sure that the Sword object wasn’t missing the sword prefab:
Screenshot 2021-11-05 101701

For the sake of completeness, here’s the complete script for the weapon scriptable object:

and the EquipWeapon method in the Fighter script:
Screenshot 2021-11-05 101852

I made sure to save the scene, then even tried shutting down Unity and restarting the file to see if the pickup would work at runtime. It still doesn’t, and I cannot see what I’ve done wrong. Any help would be really appreciated!

Take a look again at your character’s inspector, in the field named Tag:
image
The WeaponPickup looks to see if other.gameObject.tag==“Player”, but your character is untagged.
Setting the tag to Player should resolve this.

1 Like

If I set the character prefab to Player, it made all the enemies players, too. What I did instead is move the rigidbody from the character prefab to the Player prefab which solved the issue.

It seems my assumption of a child inheriting the tag was the root of the problem. Thanks for the help!

So the parent prefab of Player and Enemy should be untagged. Only the Player prefab should have the Player Tag.

Privacy & Terms