SetTrigger









How do I use Set Trigger
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: JumpAndRunMovement.cs Copy
52     void UpdateJumping()
53     {
54         if( Input.GetKey( KeyCode.Space ) == true && m_IsGrounded == true )
55         {
56             m_Animator.SetTrigger( "IsJumping" );
57             m_Body.AddForce( Vector2.up * JumpForce );
58             m_PhotonView.RPC( "DoJump", PhotonTargets.Others );
59         }
60     }
File name: JumpAndRunMovement.cs Copy
63     void DoJump()
64     {
65         m_Animator.SetTrigger( "IsJumping" );
66     }
File name: PlayerMoveScript.cs Copy
50  void Update () {
51
52   grounded = Physics2D.OverlapCircle(groundCheck.position, groundChecked, isGround);
53
54   if(transform.position.x > speedCount) {
55    speedCount += speedIncrease;
56    speedIncrease = speedIncrease * speedMultiplier;
57    moveSpeed = moveSpeed * speedMultiplier;
58   }
59
60   rigidbody.velocity = new Vector2 (moveSpeed, rigidbody.velocity.y);
61
62   if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
63
64    if(grounded) {
65     rigidbody.velocity = new Vector2 (rigidbody.velocity.x, jumpForce);
66     anim.SetTrigger ("Jump");
67     stopJumping = false;
68    }
69   }
70
71   if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stopJumping) {
72    if(jumpTimeCounter > 0) {
73     rigidbody.velocity = new Vector2 (rigidbody.velocity.x, jumpForce);
74     jumpTimeCounter -= Time.deltaTime;
75    }
76   }
77
78   if(Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp(0)) {
79    jumpTimeCounter = 0;
80    stopJumping = true;
81   }
82
83   if(grounded) {
84    jumpTimeCounter = jumpTime;
85   }
86
87
88   scoreCount += pointsPerSeconds = Time.deltaTime;
89
90   if(scoreCount > highScoreCount) {
91    highScoreCount = scoreCount;
92
93   }
94   scoreText.text = "Score: " + Mathf.Round(scoreCount);
95
96  }
File name: PlayerMoveScript.cs Copy
98  void OnCollisionEnter2D(Collision2D target) {
99   if(target.gameObject.tag == "died" || target.gameObject.tag == "Crates") {
100    jumpForce = 0;
101    scoreCount = 0;
102    anim.SetTrigger ("Died");
103    scoreText.gameObject.SetActive (false);
104    audioSource.PlayOneShot (diedClip);
105    FindObjectOfType ().gameOver (Mathf.RoundToInt(highScoreCount), coinScore);
106    FindObjectOfType ().ifPlayerDiedCoinScore(coinScore);
107    FindObjectOfType ().ifPlayerDiedScore (Mathf.RoundToInt(highScoreCount));
108   }
109  }
File name: EnemyController.cs Copy
37  void UpdateAnimationState(){
38   if(hitPoints <= 5){
39    animator.SetTrigger ("isDamage");
40   }
41  }
File name: AnimationTestScriptInspector.cs Copy
9     public override void OnInspectorGUI()
10     {
11         AnimationTestScript myTarget = (AnimationTestScript)target;
12
13         EditorGUILayout.LabelField("Set a trigger:");
14
15         foreach (AnimationTestScript.TriggerType trigger in Enum.GetValues(typeof(AnimationTestScript.TriggerType)))
16         {
17             if (GUILayout.Button(trigger.ToString()))
18             {
19                 myTarget.SetTrigger(trigger);
20             }
21         }
22
23         EditorGUILayout.Space();
24         myTarget.Speed = EditorGUILayout.Slider("Movement Speed", myTarget.Speed, 0, 10);
25     }
File name: AnimationTestScript.cs Copy
18     public void SetTrigger(TriggerType type)
19     {
20         anim.SetTrigger(type.ToString());
21     }
File name: Enemy.cs Copy
83   protected override void OnCantMove (T component)
84   {
85    //Declare hitPlayer and set it to equal the encountered component.
86    Player hitPlayer = component as Player;
87
88    //Call the LoseFood function of hitPlayer passing it playerDamage, the amount of foodpoints to be subtracted.
89    hitPlayer.LoseFood (playerDamage);
90
91    //Set the attack trigger of animator to trigger Enemy attack animation.
92    animator.SetTrigger ("enemyAttack");
93
94    //Call the RandomizeSfx function of SoundManager passing in the two audio clips to choose randomly between.
95    SoundManager.instance.RandomizeSfx (attackSound1, attackSound2);
96   }
File name: Player.cs Copy
162   protected override void OnCantMove (T component)
163   {
164    //Set hitWall to equal the component passed in as a parameter.
165    Wall hitWall = component as Wall;
166
167    //Call the DamageWall function of the Wall we are hitting.
168    hitWall.DamageWall (wallDamage);
169
170    //Set the attack trigger of the player's animation controller in order to play the player's attack animation.
171    animator.SetTrigger ("playerChop");
172   }
File name: Player.cs Copy
233   public void LoseFood (int loss)
234   {
235    //Set the trigger for the player animator to transition to the playerHit animation.
236    animator.SetTrigger ("playerHit");
237
238    //Subtract lost food points from the players total.
239    food -= loss;
240
241    //Update the food display with the new total.
242    foodText.text = "-"+ loss + " Food: " + food;
243
244    //Check to see if game has ended.
245    CheckIfGameOver ();
246   }

SetTrigger 158 lượt xem

Gõ tìm kiếm nhanh...