在Unity2D游戲開發中,玩家控制是游戲互動性的核心。本文將解析一個典型的Unity2D玩家控制腳本,探討如何實現流暢的玩家移動、跳躍和動畫切換。以下是一個Unity腳本示例,實現了這些基礎功能。
1. 腳本結構
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playe : MonoBehaviour
{public float moveSpeed = 5.0f; // 角色移動的速度public float jumpForce = 5.0f; // 角色跳躍的力public float doulbjumpForce = 5.0f;//二段跳的力public Animator animator; //動畫組件public BoxCollider2D myFeet; //碰撞體積組件public Rigidbody2D rb; // 剛體組件private bool isGrounded; // 角色是否在地面上private bool canDoubleJump;void Start(){// 獲取剛體組件//rb = GetComponent<Rigidbody2D>();//myFeet = GetComponent<BoxCollider2D>();}void CheckGround() {//定義獲取到的地面isGrounded = myFeet.IsTouchingLayers(LayerMask.GetMask("Ground"));}//用于設置角色的水平翻轉,轉向方向void Flip(){bool Has = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;if (Has){if (rb.velocity.x > 0.1f){this.gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);}if (rb.velocity.x < -0.1f){this.gameObject.transform.localRotation = Quaternion.Euler(0, 180, 0);}}}void Update(){Run();Flip();Jump();ainmatorTiao();CheckGround();}void Jump() {// 如果玩家按下空格鍵并且在地面上,則跳躍if (Input.GetButtonDown("Jump")){if (isGrounded) {//打開名為TiaoYue的動畫Bool開關,把狀態設置為trueVector2 JumpV = new Vector2(0, jumpForce);rb.velocity = Vector2.up * JumpV;canDoubleJump = true;}elseif (canDoubleJump){Vector2 doubleJump = new Vector2(0.0f, doulbjumpForce);rb.velocity = Vector2.up * doulbjumpForce;canDoubleJump = false; }}}//動畫切換void ainmatorTiao(){// 更新動畫狀態animator.SetBool("DaiJi", !Input.GetButtonDown("Jump")); // 設置待機狀態,當沒有按下跳躍鍵時為trueanimator.SetBool("TiaoYue2", false); // 默認情況下,二連跳動畫為false// 如果角色在地面上,并且沒有按下跳躍鍵,則設置待機動畫if (isGrounded && !Input.GetButtonDown("Jump")){animator.SetBool("TiaoYue", false); // 當角色在地面上且沒有按下跳躍鍵時,關閉跳躍動畫}// 如果角色按下跳躍鍵并且在地面上,則觸發第一跳動畫else if (Input.GetButtonDown("Jump") && isGrounded){animator.SetBool("DaiJi", false);animator.SetBool("TiaoYue", true); // 觸發第一跳動畫}// 如果角色在空中并且按下跳躍鍵,則觸發第二跳動畫else if (Input.GetButtonDown("Jump") && !isGrounded && !animator.GetBool("TiaoYue2")){animator.SetBool("DaiJi", false);animator.SetBool("TiaoYue", true); // 觸發第二跳動畫}}void Run() {// 獲取水平(AD鍵)的輸入float moverDir = Input.GetAxis("Horizontal");Vector2 playerVel = new Vector2(moverDir * moveSpeed, rb.velocity.y);rb.velocity = playerVel;bool Has = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;animator.SetBool("YiDong", Has);}}
?
2. 移動(Run)功能
在Run
方法中,我們根據玩家的輸入獲取水平方向上的移動速度,并應用這個速度到玩家的Rigidbody2D組件上。同時,我們檢查玩家是否在移動,以決定是否播放移動動畫。
void Run()
{// 獲取水平方向上的輸入float moverDir = Input.GetAxis("Horizontal");// 設置玩家的速度Vector2 playerVel = new Vector2(moverDir * moveSpeed, rb.velocity.y);rb.velocity = playerVel;// 檢查玩家是否在移動,并設置動畫參數bool Has = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;animator.SetBool("YiDong", Has);
}
3. 翻轉(Flip)功能
Flip
方法用于根據玩家的移動方向來翻轉玩家的方向。如果玩家向左移動,則角色朝左;如果向右移動,則角色朝右。
void Flip()
{// 檢查玩家是否有水平速度bool Has = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;if (Has){// 根據速度方向翻轉角色if (rb.velocity.x > 0.1f){this.gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);}if (rb.velocity.x < -0.1f){this.gameObject.transform.localRotation = Quaternion.Euler(0, 180, 0);}}
}
4. 跳躍(Jump)功能
在Jump
方法中,我們檢查玩家是否按下跳躍鍵,并根據玩家是否在地面上或是否可以進行雙重跳躍來應用跳躍力。
void Jump()
{// 如果玩家按下跳躍鍵if (Input.GetButtonDown("Jump")){// 如果玩家在地面上,則進行普通跳躍if (isGrounded){Vector2 JumpV = new Vector2(0, jumpForce);rb.velocity = Vector2.up * JumpV;canDoubleJump = true;}// 如果玩家不在地面上但可以進行雙重跳躍,則進行雙重跳躍else if (canDoubleJump){Vector2 doubleJump = new Vector2(0.0f, doulbjumpForce);rb.velocity = Vector2.up * doulbjumpForce;canDoubleJump = false;}}
}
5. 動畫切換(ainmatorTiao)功能
ainmatorTiao
方法用于根據玩家的狀態來切換玩家的動畫。例如,當玩家跳躍時,會播放跳躍動畫;當玩家在地面上時,會播放跑步或靜止動畫。
void ainmatorTiao()
{// 設置動畫參數以控制動畫的播放animator.SetBool("DaiJi", !Input.GetButtonDown("Jump")); // 設置待機動畫animator.SetBool("TiaoYue2", false); // 設置跳躍動畫為false// 如果玩家在地面上且沒有按下跳躍鍵,則設置跳躍動畫為falseif (isGrounded && !Input.GetButtonDown("Jump")){animator.SetBool("TiaoYue", false);}// 如果玩家按下跳躍鍵且在地面上,則設置跳躍動畫為trueelse if (Input.GetButtonDown("Jump") && isGrounded){animator.SetBool("DaiJi", false);animator.SetBool("TiaoYue", true);}// 如果玩家按下跳躍鍵且不在地面上且沒有播放第二次跳躍動畫,則設置跳躍動畫為trueelse if (Input.GetButtonDown("Jump") && !isGrounded && !animator.GetBool("TiaoYue2")){animator.SetBool("DaiJi", false);animator.SetBool("TiaoYue", true);}
}
6. 地面檢測(CheckGround)功能
CheckGround
方法用于檢測玩家是否在地面上。這通過檢查玩家的腳部碰撞體是否接觸地面層來實現。
void CheckGround()
{// 檢查玩家是否在地面上isGrounded = myFeet.IsTouchingLayers(LayerMask.GetMask("Ground"));
}
結論
通過上述腳本,我們了解了Unity2D游戲開發中實現玩家控制的基本方法。這個腳本展示了如何使用Unity的輸入系統、Rigidbody2D組件和Animator組件來實現平滑的玩家移動、跳躍和動畫切換。開發者可以根據自己的游戲需求對腳本進行修改和擴展,以實現更復雜的玩家控制邏輯。