Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KeithSeraph · Oct 12, 2016 at 06:03 AM · gameobjectwaitforsecondsienumeratorsetactiveenable

Scipt Not Functioning Properly

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








Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KeithSeraph · Oct 14, 2016 at 10:32 PM 0
Share

So it turns out my WaitForSeconds is functioning fine, it's just that the game objects will not activate and when i switch from the primary to the secondary, the lefthandik wont disable.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by b1gry4n · Oct 15, 2016 at 02:55 AM

They are doing exactly what you are telling it to. The problem is you are not making sure the coroutine is running already or not. You are activating it multiple times and as one coroutine ends, another is beginning, another is ending, etc etc. Calling the same coroutine multiple times will make multiple "instances" of that coroutine run. If you want things to happen in a linear fashion, you need to make sure only 1 thing is happening at a time. If you want to interrupt things, that functionality will need to be created too.

This is all a little chaotic and more complex than it needs to be. look in to switch case (https://msdn.microsoft.com/en-us/library/06tc147t.aspx) and build in some sort of interrupt mechanic to disable currently running coroutines.

Here is an untested example of what i mean, it should give you the idea

 bool isSwitching = false;
 int currentWeapon = 0;
 void SwitchWeapon(int desired){
 
     if(isSwitching){return;}
     if(currentWeapon == desired){return;}
     
     switch(desired){
     case 1:
         StartCoroutine(PutAway(currentWeapon,desired, 2.0f, 1.0f));
         break;
     case 2:
         StartCoroutine(PutAway(currentWeapon,desired, 2.0f, 1.75f));
         break;
     case 3:
         StartCoroutine(PutAway(currentWeapon,desired, 2.0f, 2.25f));
         break;
     default:
         Debug.Log("not valid")
         break;
     }
 }
 
 IEnumerator PutAway(int curWeap, int desiredWeap, float putAwayTime, float pullOutTime){
     isSwitching = true;
     yield return new WaitForSeconds(putAwayTime);
     if(curWeap == 1){
         primaryRenderer.enabled = false;
     }else if(curWeap == 2){
         secondaryRenderer.enabled = false;
     }else if(curWeap == 3){
         thirdRenderer.enabled = false
     }    
     StartCoroutine(PullOut(desiredWeap, pullOutTime));
 }
 
 IEnumerator PullOut(int desired, float pullOutTime){
     yield return new WaitForSeconds(pullOutTime);
     if(desired == 1){
         primaryRenderer.enabled = true;
     }else if(desired == 2){
         secondaryRenderer.enabled = true;
     }else if(desired == 3){
         thirdRenderer.enabled = true
     }    
     currentWeapon = desired;
     isSwitching = false;
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I make a game object appear a few seconds into the game? 1 Answer

Alternatives to GameObject.SetActive(...) 2 Answers

Calling setActive(false) on a disabled object? 3 Answers

works with gameObject but not with Image SetActive vs Enabled 1 Answer

Looping Blinker Doesn't work 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges