I was writing a script that basically deactivates/derenders the primary weapon when you press 5 or 2 if you have a primary equipped. Deactivates/derenders the secondary when you switch back to the primary…you get the point.
But for some reason the weapons wont activate and my WaitForSeconfds voids are not working. Please help.
I will leave the full script and reference scripts below.
I apologize if they’re a little lengthy or confusing.
Here’s the Rendering Script
using UnityEngine;
using System.Collections;
public class WeaponRendering : MonoBehaviour {
Player player;
IkHandler ikhandler;
public GameObject primary;
public GameObject secondary;
void Start ()
{
player = GetComponent<Player>();
ikhandler = GetComponent<IkHandler> ();
primary = GetComponent<GameObject> ();
secondary = GetComponent<GameObject> ();
primary.SetActive (false);
secondary.SetActive (false);
ikhandler.enabled = false;
}
void Update () {
if (player.weaponSlot == 0 && Input.GetKey("1"))
{
primarypullout ();
}
if (player.weaponSlot == 0 && Input.GetKey("2"))
{
secondarypullout ();
}
if (player.weaponSlot == 1 && Input.GetKey ("2"))
{
ikhandler.enabled = false;
switchtosecondary ();
}
if (player.weaponSlot == 1 && Input.GetKey ("5"))
{
ikhandler.enabled = false;
unequipprimary ();
}
if (player.weaponSlot == 2 && Input.GetKey ("1"))
{
switchtoprimary ();
}
if (player.weaponSlot == 2 && Input.GetKey ("5"))
{
unequipsecondary ();
}
}
public void primarypullout()
{
StartCoroutine(primaryequipwait());
}
public void secondarypullout()
{
StartCoroutine (secondarywait ());
}
public void switchtoprimary()
{
StartCoroutine (secondaryawaywait ());
}
public void switchtosecondary()
{
StartCoroutine (primaryawaywait ());
}
public void unequipprimary()
{
StartCoroutine (primaryputbackwait ());
}
public void unequipsecondary()
{
StartCoroutine (secondaryputbackwait ());
}
IEnumerator primaryequipwait()
{
yield return new WaitForSeconds (2);
renderprimary ();
}
IEnumerator secondarywait()
{
yield return new WaitForSeconds (0.7f);
rendersecondary ();
}
IEnumerator primaryawaywait()
{
yield return new WaitForSeconds (2);
derenderprimary ();
}
IEnumerator secondaryawaywait()
{
yield return new WaitForSeconds (0.7f);
derendersecondary ();
}
IEnumerator rifleikwait()
{
yield return new WaitForSeconds (2);
ikhandler.enabled = true;
}
IEnumerator secondaryfpwait()
{
yield return new WaitForSeconds (1.5f);
rendersecondary ();
}
IEnumerator primaryputbackwait()
{
yield return new WaitForSeconds (2);
primaryaway ();
}
IEnumerator secondaryputbackwait()
{
yield return new WaitForSeconds (0.7f);
secondaryputback ();
}
public void renderprimary()
{
StartCoroutine (rifleikwait ());
primary.SetActive (true);
}
public void rendersecondary()
{
secondary.SetActive (true);
}
public void derenderprimary()
{
StartCoroutine (secondaryfpwait ());
primary.SetActive (false);
}
public void derendersecondary()
{
StartCoroutine (primaryequipwait ());
}
public void primaryaway()
{
primary.SetActive (false);
}
public void secondaryputback()
{
secondary.SetActive (false);
}
}
Here’s the Player Script
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
IkHandler ikhandler;
public Animator anim;
public Rigidbody rbody;
public float inputH;
public float inputV;
public bool run;
public bool reloading;
public bool shooting;
public bool aiming;
public bool crouch;
public int weaponSlot;
public Vector3 lookPosition;
public Vector3 lookHitPosition;
public LayerMask layerMask;
public CharacterAudioManager audioManager;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody>();
ikhandler = GetComponent<IkHandler> ();
audioManager = GetComponent<CharacterAudioManager> ();
run = false;
crouch = false;
aiming = false;
shooting = false;
reloading = false;
ikhandler.enabled = false;
weaponSlot = 0;
}
// Update is called once per frame
public void Update ()
{
if(Input.GetKey(KeyCode.LeftShift))
{
run = true;
}
else
{
run = false;
}
if(Input.GetKey(KeyCode.Space))
{
anim.SetBool("jump", true);
}
else
{
anim.SetBool("jump", false);
}
if(Input.GetKey(KeyCode.LeftControl))
{
anim.SetBool("crouch", true);
}
else
{
anim.SetBool("crouch", false);
}
if(Input.GetMouseButton(1))
{
anim.SetBool("aiming", true);
}
else
{
anim.SetBool("aiming", false);
}
if (Input.GetMouseButton (2))
{
anim.SetBool ("shooting", true);
}
else
{
anim.SetBool ("shooting", false);
}
if(Input.GetKey("r"))
{
anim.SetBool("reloading", true);
}
else
{
anim.SetBool("reloading", false);
}
if (Input.GetKey("1"))
{
anim.SetInteger ("weaponSlot", 1);
}
if (Input.GetKey("2"))
{
anim.SetInteger("weaponSlot",2);
}
if (Input.GetKey("5"))
{
anim.SetInteger("weaponSlot",0);
}
inputH = Input.GetAxis("Horizontal");
inputV = Input.GetAxis("Vertical");
anim.SetFloat("inputH",inputH);
anim.SetFloat("inputV",inputV);
anim.SetBool("run", run);
float moveX = inputH * -50f * Time.deltaTime;
float moveZ = inputV * -50f * Time.deltaTime;
if(run)
{
moveX *= 3f;
moveZ *= 3f;
}
rbody.velocity = new Vector3(moveX,0f,moveZ);
}
}
Here’s the Ik Script (Really doesn’t cause a problem here, because it just needs to be activated and deactivated on call, but just in case)
using UnityEngine;
using System.Collections;
public class IkHandler : MonoBehaviour {
Animator anim;
Player player;
public float lookWeight = 1;
public float bodyWeight = 0.8f;
public float headWeight = 1;
public float clampWeight = 1;
float targetWeight;
public Transform weaponHolder;
public Transform rightShoulder;
public Transform overrideLookTarget;
public Transform rightHandIkTarget;
public float rightHandIkWeight;
public Transform leftHandIkTarget;
public float leftHandIkWeight;
Transform aimHelper;
void Start() {
aimHelper = new GameObject ().transform;
anim = GetComponent<Animator> ();
player = GetComponent<Player> ();
}
void FixedUpdate()
{
if (rightShoulder == null)
{
rightShoulder = anim.GetBoneTransform (HumanBodyBones.RightShoulder);
}
else
{
weaponHolder.position = rightShoulder.position;
}
if (player.aiming && !player.reloading)
{
Vector3 directionTowardsTarget = aimHelper.position - transform.position;
float angle = Vector3.Angle (transform.forward, directionTowardsTarget);
if (angle < 90)
{
targetWeight = 1;
}
else
{
targetWeight = 0;
}
}
else
{
targetWeight = 0;
}
float multiplier = (player.aiming) ? 5 : 30;
lookWeight = Mathf.Lerp (lookWeight, targetWeight, Time.deltaTime * multiplier);
rightHandIkWeight = lookWeight;
leftHandIkWeight = 1 - anim.GetFloat ("LeftHandIKWeightOverride");
HandleShoulderRotation ();
}
void HandleShoulderRotation()
{
aimHelper.position = Vector3.Lerp (aimHelper.position, player.lookPosition, Time.deltaTime * 5);
weaponHolder.LookAt (aimHelper.position);
rightHandIkTarget.parent.transform.LookAt (aimHelper.position);
}
void OnAnimatorIK()
{
anim.SetLookAtWeight (lookWeight, bodyWeight, headWeight, headWeight, clampWeight);
Vector3 filterDirection = player.lookPosition;
//filterDirection.y = offsetY; if needed
anim.SetLookAtPosition (
(overrideLookTarget != null) ?
overrideLookTarget.position : filterDirection
);
if (leftHandIkTarget)
{
anim.SetIKPositionWeight (AvatarIKGoal.LeftHand, leftHandIkWeight);
anim.SetIKPosition (AvatarIKGoal.LeftHand, leftHandIkTarget.position);
anim.SetIKRotationWeight (AvatarIKGoal.LeftHand, leftHandIkWeight);
anim.SetIKRotation (AvatarIKGoal.LeftHand, leftHandIkTarget.rotation);
}
if (rightHandIkTarget)
{
anim.SetIKPositionWeight (AvatarIKGoal.RightHand, rightHandIkWeight);
anim.SetIKPosition (AvatarIKGoal.RightHand, rightHandIkTarget.position);
anim.SetIKRotationWeight (AvatarIKGoal.RightHand, rightHandIkWeight);
anim.SetIKRotation (AvatarIKGoal.RightHand, rightHandIkTarget.rotation);
}
}
}