1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets._2D
5 {
6     
public class PlatformerCharacter2D : MonoBehaviour
7     {
8         [SerializeField]
private float m_MaxSpeed = 10f; // The fastest the player can travel in the x axis.
9         [SerializeField]
private float m_JumpForce = 400f; // Amount of force added when the player jumps.
10         [Range(
0, 1)] [SerializeField] private float m_CrouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
11         [SerializeField]
private bool m_AirControl = false; // Whether or not a player can steer while jumping;
12         [SerializeField]
private LayerMask m_WhatIsGround; // A mask determining what is ground to the character
13
14         
private Transform m_GroundCheck; // A position marking where to check if the player is grounded.
15         
const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
16         
private bool m_Grounded; // Whether or not the player is grounded.
17         
private Transform m_CeilingCheck; // A position marking where to check for ceilings
18         
const float k_CeilingRadius = .01f; // Radius of the overlap circle to determine if the player can stand up
19         
private Animator m_Anim; // Reference to the player's animator component.
20         
private Rigidbody2D m_Rigidbody2D;
21         
private bool m_FacingRight = true; // For determining which way the player is currently facing.
22
23         
private void Awake()
24         {
25             
// Setting up references.
26             m_GroundCheck = transform.Find(
"GroundCheck");
27             m_CeilingCheck = transform.Find(
"CeilingCheck");
28             m_Anim = GetComponent<Animator>();
29             m_Rigidbody2D = GetComponent<Rigidbody2D>();
30         }
31
32
33         
private void FixedUpdate()
34         {
35             m_Grounded =
false;
36
37             
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
38             
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
39             Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
40             
for (int i = 0; i < colliders.Length; i++)
41             {
42                 
if (colliders[i].gameObject != gameObject)
43                     m_Grounded =
true;
44             }
45             m_Anim.SetBool(
"Ground", m_Grounded);
46
47             
// Set the vertical animation
48             m_Anim.SetFloat(
"vSpeed", m_Rigidbody2D.velocity.y);
49         }
50
51
52         
public void Move(float move, bool crouch, bool jump)
53         {
54             
// If crouching, check to see if the character can stand up
55             
if (!crouch && m_Anim.GetBool("Crouch"))
56             {
57                 
// If the character has a ceiling preventing them from standing up, keep them crouching
58                 
if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
59                 {
60                     crouch =
true;
61                 }
62             }
63
64             
// Set whether or not the character is crouching in the animator
65             m_Anim.SetBool(
"Crouch", crouch);
66
67             
//only control the player if grounded or airControl is turned on
68             
if (m_Grounded || m_AirControl)
69             {
70                 
// Reduce the speed if crouching by the crouchSpeed multiplier
71                 move = (crouch ? move*m_CrouchSpeed : move);
72
73                 
// The Speed animator parameter is set to the absolute value of the horizontal input.
74                 m_Anim.SetFloat(
"Speed", Mathf.Abs(move));
75
76                 
// Move the character
77                 m_Rigidbody2D.velocity =
new Vector2(move*m_MaxSpeed, m_Rigidbody2D.velocity.y);
78
79                 
// If the input is moving the player right and the player is facing left...
80                 
if (move > 0 && !m_FacingRight)
81                 {
82                     
// ... flip the player.
83                     Flip();
84                 }
85                     
// Otherwise if the input is moving the player left and the player is facing right...
86                 
else if (move < 0 && m_FacingRight)
87                 {
88                     
// ... flip the player.
89                     Flip();
90                 }
91             }
92             
// If the player should jump...
93             
if (m_Grounded && jump && m_Anim.GetBool("Ground"))
94             {
95                 
// Add a vertical force to the player.
96                 m_Grounded =
false;
97                 m_Anim.SetBool(
"Ground", false);
98                 m_Rigidbody2D.AddForce(
new Vector2(0f, m_JumpForce));
99             }
100         }
101
102
103         
private void Flip()
104         {
105             
// Switch the way the player is labelled as facing.
106             m_FacingRight = !m_FacingRight;
107
108             
// Multiply the player's x local scale by -1.
109             Vector3 theScale = transform.localScale;
110             theScale.x *= -
1;
111             transform.localScale = theScale;
112         }
113     }
114 }


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