1
\$\begingroup\$

I am trying to follow this tutorial video about setting up an third-person shooter character in Unity.

But my character will not hold their weapon. I'm also getting the following warnings in my project:

Assets/Unity-1/unity-1/player/playerAnimation.cs(8, 23): warning CS0649: Field `playerAnimation.aimMask' is never assigned to, and will always have its default value.

Assets/Unity-1/unity-1/player/playerAnimation.cs(16, 23): warning CS0414: The private field `playerAnimation.chestBone' is assigned but its value is never used.

Assets/Unity-1/unity-1/player/playerLocomotion.cs(12, 19): warning CS0414: The private field `playerLocomotion.aimedWalkSpeed' is assigned but its value is never used.

Unity editor screenshot showing warnings

How can I fix this?

Here are the scripts that the warnings complain about:

Player Animation:

/// <license>
/// Copyright(c) 2020 www.GameDevine.com
/// </license>
///
using UnityEngine;

public class playerAnimation : MonoBehaviour
{
   private LayerMask aimMask;
   private playerLocomotion playerLocomtion = null;
   private playerCamera playerCamera = null;
   private MovementCalculator movementCalculator = null;
   private playerWeapon playerWeapon = null;

   private Animator animator = null;
   [System.NonSerialized] public bool readyToMove = true;
   private Transform chestBone = null;
   [System.NonSerialized] public Vector3 trgetPoint = Vector3.zero;

   private float handIkSpeed = 10;
   private Transform rHandIk = null;
   private Transform lHandIk = null;
   private Transform rElbowIk = null;
   private Transform lElbowIk = null;


   private void Awake()
   {
      playerLocomtion = GetComponent<playerLocomotion>();
      playerCamera = GetComponent<playerCamera>();
      movementCalculator = GetComponent<MovementCalculator>();
      animator = GetComponent<Animator>();
      playerWeapon = GetComponent<playerWeapon>();
      rHandIk = new GameObject("rhik").transform;
      lHandIk = new GameObject("lhik").transform;
      rElbowIk = new GameObject("reik").transform;
      lElbowIk = new GameObject("leik").transform;
      Transform p = animator.GetBoneTransform(HumanBodyBones.Chest);
      rHandIk.parent = p;
      lHandIk.parent = p;
      rElbowIk.parent = p;
      lElbowIk.parent = p;
  }
  public void InitializeWeapon()
  {
     if(playerWeapon.weapon != null)
     {
        if (!rHandIk)
        {
           rHandIk = new GameObject("rhik").transform;
        }
        if (!lHandIk)
        {
           lHandIk = new GameObject("lhik").transform;
        }
        rHandIk.parent = playerWeapon.weapon.model;
        lHandIk.parent = playerWeapon.weapon.model;
     }
  }


  private void Update()
  {
     trgetPoint = GetAimTarget(50);
     animator.SetBool("Crouch", playerLocomtion.crouched);
     animator.SetBool("aimed" , playerLocomtion.aiming);
     animator.SetBool("grounded", playerLocomtion.grounded);
     animator.SetFloat("moveX", movementCalculator.Movement.x);
     animator.SetFloat("moveY", movementCalculator.Movement.z);
     switch (playerLocomtion.MoveType)
     {
         case 1:
               animator .SetBool("Walk" , true);
               animator .SetBool("Sprint", false);
               animator .SetBool("Run" , false);
               break;
         case 2:
               animator.SetBool("Walk" , false);
               animator.SetBool("Sprint", false);
               animator.SetBool("Run" , true);
               break;
         case 3:
               animator.SetBool("Walk" , false);
               animator.SetBool("Sprint", true);
               animator.SetBool("Run" , false);
               break;
         default:
              animator.SetBool("Walk" , false);
              animator.SetBool("Sprint", false);
              animator.SetBool("Run" , false);
              break;

       }
       readyToMove = !animator.GetCurrentAnimatorStateInfo(0).IsTag("Transition");
    }
    private Vector3 GetAimTarget(float range)
    {
       Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
       ray.origin = playerCamera.pivot.position;
       RaycastHit hit;
       if(Physics.Raycast(ray, out hit, range, aimMask))
       {
          return hit.point;

       }
       else
       {
          return playerCamera.pivot.position + playerCamera.main.forward.normalized * range;
       }

    }
 }

Player Locomotion:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


public class playerLocomotion : MonoBehaviour
{
   private float walkSpeed = 4f;
   private float runSpeed = 7f;
   private float sprintSpeed = 10f;
   private float aimedWalkSpeed = 4f;
   private float crouchWalkSpeed = 3f;
   private float rotateSpeed = 10f;
   private float aimedRotateSpeed = 20;

   private playerCamera playerCamera = null;
   private playerinput playerinput = null;
   playerAnimation playerAnimation = null;
  [NonSerialized] public bool grounded = true;
   private float movement = 0f;
   private int moveType = 0;
  [NonSerialized] public bool crouched = false;
  [NonSerialized] public bool aiming = false;


   public int MoveType
   {
      get
      {
         if (movement < 0.1f)
         {
            return 0;
         }
         else
         {
           return moveType;
         }
     }
  }

  private void Awake()
  {
    playerCamera = GetComponent<playerCamera>();
    playerinput = GetComponent<playerinput>();
    playerAnimation = GetComponent<playerAnimation>();
  }

  private void Update()
  {
     float ver = playerinput.vertical;
     float hor = playerinput.horizontal;
     if (!playerAnimation.readyToMove)
     {
        ver = 0;
        hor = 0;

     }
     if(playerinput.crouch && grounded && !playerinput.sprint)
     {
       crouched = !crouched;
     }
     movement = Mathf.Clamp01(Mathf.Abs(hor) + Mathf.Abs(ver));
     if (grounded)
     {
        Vector3 cameraDirection = playerCamera.main.forward * ver;
        cameraDirection += playerCamera.main.right * hor;
        Vector3 dir = Vector3.zero;
        if (playerinput.jump)
        {

        }
        else
        {
            if (aiming)
            {

            }
            else
            {

 
               #region Rotation
               cameraDirection.Normalize();
               cameraDirection.y = 0;
               if(cameraDirection == Vector3.zero)
               {
                  cameraDirection = transform.forward;
               }
               Quaternion quaternion = Quaternion.LookRotation(cameraDirection);
               Quaternion targetRotation = Quaternion .Slerp (transform .rotation ,quaternion, Time.
               deltaTime * movoement * rotateSpeed);
               transform.rotation = targetRotation;                                                             
               #endregion
               

               #region Movement
               float speed = runSpeed;
               moveType = 2;
               if (playerinput.sprint)
               {
                  speed = sprintSpeed;
                  moveType = 3;

               }
               else if (playerinput.Walk)
               {
                  speed = walkSpeed;
                  moveType = 1;
               }
               if (crouched)
               {
                  speed = crouchWalkSpeed;
               }
               Vector3 targetPosition = transform.forward * movement * speed / 100;
               targetPosition.y = 0;
               transform.Translate(targetPosition, Space.World);
               #endregion

           }

       }
       else
       {

     }
  }

}

Player weapon:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerWeapon : MonoBehaviour
{
    public Weapon weapon = null;
    private playerLocomotion playerLocomotion = null;
    playerAnimation playerAnimation = null;
    playerCamera playerCamera = null;
    public float moveSpeed = 10;
    public float rotateSpeed = 10;
    private Weapon.Pose pose = Weapon.Pose.stndingNormal;
    public Weapon.Properties.Pose poseData = new Weapon.Properties.Pose();

    private void Awake()
    {
        playerCamera = GetComponent<playerCamera>();
        playerAnimation = GetComponent<playerAnimation>();
        playerLocomotion = GetComponent<playerLocomotion>();

    }
    
    private void Satart()
    {
        if (weapon)
        {
            Initialize();
        }     
    }

    public void Initialize()
    {
        Initialize(Vector3.zero);
    }

    public void Initialize(Vector3 zero)
    {
        weapon.transform.parent = GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Chest);
        weapon.transform.localPosition = Vector3.zero;
        weapon.transform.localEulerAngles = Vector3.zero;
        poseData = weapon.GetPose(pose);
        weapon.model.localPosition = poseData.pose.position;
        weapon.model.localRotation = poseData.pose.rotation;
    }

    private void Update()
    {
        if (!weapon)
        { 
          return;
        }
        if (playerLocomotion.crouched)
        {
            if (playerLocomotion.aiming)
            {
              pose = Weapon.Pose.crouchedAimed;
            }
            else
            {
              pose = Weapon.Pose.crouchedNormal;
            }    
            
        }
        else
        {
           if (playerLocomotion.aiming)
            { 
              pose = Weapon.Pose.standingAimed;
            }
            else
            {
              pose = Weapon.Pose.stndingNormal;
            } 
        }
        poseData = weapon.GetPose(pose);
        if(weapon.model.position != poseData.pose.position)
        {
          weapon.model.position = Vector3.Lerp(weapon.model.position, poseData.pose.position, moveSpeed * Time.deltaTime);
        }
        if(weapon.model.rotation != poseData.pose.rotation)
        {
          weapon.model.rotation = Quaternion.Lerp(weapon.model.rotation, poseData.pose.rotation, rotateSpeed * Time.deltaTime);
        }
        
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ I understood you, but I tried it \$\endgroup\$ Commented Oct 14, 2021 at 15:39

1 Answer 1

2
\$\begingroup\$

In the class playerWeapon, you have this method:

private void Satart()

I think this is supposed to mean Start. The Start method gets executed automatically on the first frame of the objects existence, but you have to spell it correctly. When you misspell a Unity event method, then it won't get recognized by the engine and the engine won't call it.

The result is that Initialize won't get called either, which means that the weapon model never gets parented to the armature of the character.

I don't know what IDE you are using for editing your code, but I would recommend Visual Studio 2019. When you create a Unity event method, then it will show a small Unity-icon above it. When you misspell the method name, then that icon won't show up and you get a warning instead telling you you created a private method which is not used anywhere.


The warnings you receive about unused variables appear to be unrelated to this particular problem. I would assume that you were told to create those variables in preparation of features which come later in the tutorial you are following.

Although if your character has a problem with aiming at things you are pointing at, then the first one "playerAnimation.aimMask is never assigned to" might be worth investigating. The value is later used in the method GetAimTarget to determine what colliders are considered valid objects to aim at via Physics.Raycast.

\$\endgroup\$
1
  • \$\begingroup\$ I understood you, but I tried it \$\endgroup\$ Commented Oct 14, 2021 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .