在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开关,把状态设置为true
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;
}
}
}
//动画切换
void ainmatorTiao()
{
// 更新动画状态
animator.SetBool("DaiJi", !Input.GetButtonDown("Jump")); // 设置待机状态,当没有按下跳跃键时为true
animator.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
// 如果玩家在地面上且没有按下跳跃键,则设置跳跃动画为false
if (isGrounded && !Input.GetButtonDown("Jump"))
{
animator.SetBool("TiaoYue", false);
}
// 如果玩家按下跳跃键且在地面上,则设置跳跃动画为true
else if (Input.GetButtonDown("Jump") && isGrounded)
{
animator.SetBool("DaiJi", false);
animator.SetBool("TiaoYue", true);
}
// 如果玩家按下跳跃键且不在地面上且没有播放第二次跳跃动画,则设置跳跃动画为true
else 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组件来实现平滑的玩家移动、跳跃和动画切换。开发者可以根据自己的游戏需求对脚本进行修改和扩展,以实现更复杂的玩家控制逻辑。