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.
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);
}
}
}