# unity-2d 转向

首先,unity-2d 其实也处于一个 3d 的世界当中,只不过所有的元素都是平面状的,所以,我们可以通过改变方向到 180 度来实现人物转向功能

  1. 创建变量
    row
    1
    2
    3
    private int facingDir = 1;  // 面向方向(1为右,-1为左)
    private bool facingRight = true; // 是否朝右
    private float xinput; // 水平方向输入
  2. 然后,创建反转角色函数
    ROW
    1
    2
    3
    4
    5
    6
    7
    // 角色翻转
    private void Flip()
    {
    facingDir = -1 * facingDir; // 反转面向方向
    facingRight = !facingRight; // 切换朝向
    transform.Rotate(0, 180, 0); // 旋转角色,使其面向另一方向
    }
  3. 控制翻转行为
    row
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    // 控制翻转行为
    private void FlipController()
    {
    if (rb.velocity.x > 0 && !facingRight) // 如果角色向右移动且当前面朝左
    {
    Flip(); // 执行翻转
    }
    else if (rb.velocity.x < 0 && facingRight) // 如果角色向左移动且当前面朝右
    {
    Flip(); // 执行翻转
    }
    }

# unity-2d 限制跳跃次数

  1. 创建新的变量与标头,使可读性更高

    row
    1
    2
    3
    4
    [Header("碰撞检测信息")]
    [SerializeField] private float groundCheckDistance; // 地面检测距离
    [SerializeField] private LayerMask whatIsGround; // 表示地面的层级
    private bool isGrounded; // 是否在地面上

  2. 碰撞检测

    row
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // 碰撞检测(检测是否在地面上)
    private void CollisionChecks()
    {
    isGrounded = Physics2D.Raycast(transform.position, Vector2.down, groundCheckDistance, whatIsGround); // 投射射线检测地面
    }

    // 绘制Gizmos,用于显示射线
    private void OnDrawGizmos()
    {
    Gizmos.DrawLine(transform.position, new Vector3(transform.position.x, transform.position.y - groundCheckDistance)); // 在编辑器中显示检测射线
    }

  3. 限制 jump 行为

    row
    1
    2
    3
    4
    5
    private void Jump()
    {
    if (isGrounded) // 检查是否在地面上
    rb.velocity = new Vector2(rb.velocity.x, jumpforce); // 添加向上的跳跃力
    }

    在 c# 中,只有一行的 if 语句是可以直接写的

  4. Physics2D.Raycast 函数使用:

    row
    1
    RaycastHit2D hitInfo = Physics2D.Raycast(origin, direction, distance, layerMask);

参数详解

  1. origin (Vector2) - 射线的起点。
    • 通常用物体的位置,比如 transform.position
  2. direction (Vector2) - 射线的方向。
    • 这是一个二维向量,表示射线的投射方向。例如, Vector2.down 表示向下, Vector2.right 表示向右。
  3. distance (float) - 射线的最大检测距离(可选参数)。
    • 这个参数设定了射线的长度。如果射线超过这个长度没有检测到任何碰撞物,就会返回一个空的 RaycastHit2D
  4. layerMask (int) - 层掩码 (LayerMask)(可选参数)。
    • 用来指定射线可以检测的层(Layer)。如果只想检测特定的物体,比如地面或墙壁,可以用 LayerMask 来过滤掉其他不相关的物体。
    • 例如, LayerMask.GetMask("Ground") 可以创建一个只检测 "Ground" 层的掩码。

# 代码总览

row
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
// 定义变量
private Rigidbody2D rb; // 2D刚体,用于物理控制
[SerializeField] private Animator animator; // 动画控制器
[SerializeField] private float moveSpeed; // 移动速度
[SerializeField] private float jumpforce; // 跳跃力度
private int facingDir = 1; // 面向方向(1为右,-1为左)
private bool facingRight = true; // 是否朝右
private float xinput; // 水平方向输入

[Header("碰撞检测信息")]
[SerializeField] private float groundCheckDistance; // 地面检测距离
[SerializeField] private LayerMask whatIsGround; // 表示地面的层级
private bool isGrounded; // 是否在地面上

// Start方法在游戏开始时调用
void Start()
{
rb = GetComponent<Rigidbody2D>(); // 获取2D刚体组件
animator = GetComponentInChildren<Animator>(); // 获取子对象中的动画组件
}

// Update方法在每帧调用一次
void Update()
{
Movement(); // 控制角色移动
CheckInput(); // 检查玩家输入
CollisionChecks(); // 碰撞检测(是否在地面上)
FlipController(); // 控制角色翻转
AnimatorController(); // 控制动画
}

// 检查玩家输入
private void CheckInput()
{
xinput = Input.GetAxisRaw("Horizontal"); // 获取水平输入
if (Input.GetKeyDown(KeyCode.Space)) // 检测空格键跳跃
{
Jump(); // 执行跳跃
Debug.Log("jump"); // 输出跳跃信息到控制台
}
}

// 控制角色移动
private void Movement()
{
// 更新角色的水平速度
rb.velocity = new Vector2(xinput * moveSpeed, rb.velocity.y);
}

// 跳跃方法
private void Jump()
{
if (isGrounded) // 检查是否在地面上
rb.velocity = new Vector2(rb.velocity.x, jumpforce); // 添加向上的跳跃力
}

// 控制动画状态
private void AnimatorController()
{
bool moving = rb.velocity.x != 0; // 判断角色是否在移动
animator.SetBool("moving", moving); // 设置动画的"moving"参数
}

// 角色翻转
private void Flip()
{
facingDir = -1 * facingDir; // 反转面向方向
facingRight = !facingRight; // 切换朝向
transform.Rotate(0, 180, 0); // 旋转角色,使其面向另一方向
}

// 控制翻转行为
private void FlipController()
{
if (rb.velocity.x > 0 && !facingRight) // 如果角色向右移动且当前面朝左
{
Flip(); // 执行翻转
}
else if (rb.velocity.x < 0 && facingRight) // 如果角色向左移动且当前面朝右
{
Flip(); // 执行翻转
}
}

// 碰撞检测(检测是否在地面上)
private void CollisionChecks()
{
isGrounded = Physics2D.Raycast(transform.position, Vector2.down, groundCheckDistance, whatIsGround); // 投射射线检测地面
}

// 绘制Gizmos,用于显示射线
private void OnDrawGizmos()
{
Gizmos.DrawLine(transform.position, new Vector3(transform.position.x, transform.position.y - groundCheckDistance)); // 在编辑器中显示检测射线
}
}