从零开始的类银河恶魔城游戏04
[自学]从零开始的类银河恶魔城游戏04
part 4:Collision Check 2025/2/4
碰撞检测射线

- 在Player.cs中编写射线函数OnDrawGizmos()
[Header("Collision info")]
[SerializeField]private Transform groundCheck;
[SerializeField]private float groundCheckDistance;
[SerializeField]private Transform wallCheck;
[SerializeField]private float wallCheckDistance;
[SerializeField]private LayerMask whatIsGround;
[SerializeField]private LayerMask whatIsWall;
//地面检测
//Physics2D.RayCast(起始点,方向,距离,检测层)
public bool IsGroundedDetected() => Physics2D.Raycast(groundCheck.position,Vector2.down,groundCheckDistance,whatIsGround);
//画碰撞检测射线
private void OnDrawGizmos()
{
//Gizmos.DrawLine(起始点,终点(x,y-距离))
//右上+,左下-
Gizmos.DrawLine(groundCheck.position,new Vector3(groundCheck.position.x,groundCheck.position.y-groundCheckDistance));
Gizmos.DrawLine(wallCheck.position,new Vector3(wallCheck.position.x+wallCheckDistance,wallCheck.position.y));
}
2.创建两个empty:GroundCheck、WallCheck,拖入Player脚本Player.cs中的对应参数,适当调整位置。
代码:
//Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Callbacks;
using UnityEngine;
public class Player: MonoBehaviour
{
[Header("Move info")]
public float moveSpeed = 8f;
public float jumpForce = 15f;
[Header("Collision info")]
[SerializeField]private Transform groundCheck;
[SerializeField]private float groundCheckDistance;
[SerializeField]private Transform wallCheck;
[SerializeField]private float wallCheckDistance;
[SerializeField]private LayerMask whatIsGround;
[SerializeField]private LayerMask whatIsWall;
#region Components
public Animator anim { get; private set; }
public Rigidbody2D rb { get; private set; }
#endregion
#region States
public PlayerStateMachine stateMachine { get; private set; }
public PlayerIdleState idleState { get; private set; }
public PlayerMoveState moveState { get; private set; }
public PlayerJumpState jumpState { get; private set; }
public PlayerAirState airState { get;private set; }
#endregion
private void Awake()
{
stateMachine = new PlayerStateMachine();
idleState = new PlayerIdleState(this,stateMachine,"Idle");
moveState = new PlayerMoveState(this,stateMachine,"Move");
jumpState = new PlayerJumpState(this,stateMachine,"Jump");
airState = new PlayerAirState(this,stateMachine, "Jump");
}
public void Start()
{
anim = GetComponentInChildren<Animator>();
rb = GetComponent<Rigidbody2D>();
stateMachine.Initialize(idleState);// PlayerStateMachine.Initialize()
}
public void Update()
{
stateMachine.currentState.Update();// PlayerState.Update()
}
//设置速度
public void SetVelocity(float _xVelocity, float _yVelocity)
{
rb.velocity = new Vector2(_xVelocity,_yVelocity);
}
//地面检测
//Physics2D.RayCast(起始点,方向,距离,检测层)
public bool IsGroundedDetected() => Physics2D.Raycast(groundCheck.position,Vector2.down,groundCheckDistance,whatIsGround);
//画碰撞检测射线
private void OnDrawGizmos()
{
//Gizmos.DrawLine(起始点,终点(x,y-距离))
//右上+,左下-
Gizmos.DrawLine(groundCheck.position,new Vector3(groundCheck.position.x,groundCheck.position.y-groundCheckDistance));
Gizmos.DrawLine(wallCheck.position,new Vector3(wallCheck.position.x+wallCheckDistance,wallCheck.position.y));
}
}
//PlayerGroundedState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerGroundedState : PlayerState
{
public PlayerGroundedState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
{
}
public override void Enter()
{
base.Enter();
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
if (Input.GetKeyDown(KeyCode.Space) && player.IsGroundedDetected())
{
stateMachine.ChangeState(player.jumpState);
}
}
}
//PlayerAirState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAirState : PlayerState
{
public PlayerAirState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
{
}
public override void Enter()
{
base.Enter();
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
if (player.IsGroundedDetected())
{
stateMachine.ChangeState(player.idleState);
}
}
}
//其他

浙公网安备 33010602011771号