Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

[SIMPLE] Layer Weight Change Animator

Discussion in 'Scripting' started by eleon-games, Jan 14, 2018.

  1. eleon-games

    eleon-games

    Joined:
    Feb 19, 2015
    Posts:
    56
    Hey,

    i want to Set the Layer Weight and it is working on my Test Cube
    But if i duplicate the Cube, then it is only working on one of them.
    If i untick one Cube the other works again. But never both. Dont know why. Any Ideas?

    Thank you very much for your Time

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestLayerChange : MonoBehaviour
    6. {
    7.    public Animator animator;                    // Assign Animator
    8.    public bool PlayerTrigger = false;       // Set Bool if Player enter the Trigger
    9.  
    10.     void Update ()
    11.     {
    12.         if (PlayerTrigger)
    13.         {
    14.             animator.SetLayerWeight (3, 1f);   // If Player enter Trigger then Set the Layer Weight to 1
    15.         }
    16.         else
    17.         {
    18.             animator.SetLayerWeight (3, 0f);   // If Player leave Trigger then Set the Layer Weight to 1
    19.         }
    20.     }
    21.  
    22.     void OnTriggerEnter(Collider other)
    23.     {
    24.         if (other.tag == "Player")
    25.         {
    26.             PlayerTrigger = true;
    27.         }
    28.     }
    29.  
    30.     void OnTriggerExit(Collider other)
    31.     {
    32.         if (other.tag == "Player")
    33.         {
    34.             PlayerTrigger = false;
    35.         }
    36.     }
    37. }
    38.  
     
    Last edited: Jan 14, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    well, what is 'otherObject' ? Are you possibly setting the value on the same animator (twice)?
     
  3. eleon-games

    eleon-games

    Joined:
    Feb 19, 2015
    Posts:
    56
    Thanks for the response. I did not need the Start Function so i deleted that line.
    But i have the same Problem. What do you mean with setting it twice on the same animator? Maybe thats why, but how else can i do it?

    Maybe the picture get a better understanding of my problem
     
    Last edited: Jan 14, 2018
  4. eleon-games

    eleon-games

    Joined:
    Feb 19, 2015
    Posts:
    56
    Okay got it. No need of Update either. Thanks
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, I'm actually not sure. Though, I think you could set the weight in the enter/exit instead of polling for it in Update, if you do figure out why it's not working. :)

    lol. You responded as I was still wondering. Post your solution or write what was wrong/how you fixed it? :)
     
  6. eleon-games

    eleon-games

    Joined:
    Feb 19, 2015
    Posts:
    56
    I just put it in the OnTriggerEnter/Exit function and it now works for all Prefabs :D

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestLayerChange : MonoBehaviour {
    6.  
    7.  
    8.     public Animator animator;                       // Assign Animator
    9.     public bool PlayerTrigger = false;       // Set Bool if Player enter the Trigger
    10.  
    11.  
    12.  
    13.     void OnTriggerEnter(Collider other)
    14.     {
    15.         if (other.tag == "Player")
    16.         {
    17.             PlayerTrigger = true;
    18.             animator.SetLayerWeight (3, 1f);
    19.         }
    20.     }
    21.  
    22.     void OnTriggerExit(Collider other)
    23.     {
    24.         if (other.tag == "Player")
    25.         {
    26.             PlayerTrigger = false;
    27.             animator.SetLayerWeight (3, 0f);
    28.         }
    29.     }
    30. }
    31.  
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Interesting. Maybe the variable was not set properly when you copied the other one.
    Anyways, it's a good fix as moving it to that portion of the code is more sensible anyways ;)