2dRogueLike源码分析MovingObject

本文分析了2D RogueLike游戏中移动对象的实现,从创建抽象类到具体的移动检测和处理。内容包括:定义移动对象的属性,如移动时间、碰撞层、包围盒和刚体;初始化和移动函数的实现,利用Linecast进行碰撞检测;通过协程实现平滑移动;定义抽象函数OnCantMove处理无法移动时的碰撞反馈。
摘要由CSDN通过智能技术生成

1.导入unity包

using UnityEngine;
using System.Collections;

2.创建抽象类,给enemy和player继承

定义一些属性:移动的时间,位于什么层(检测碰撞),包围盒,刚体(应用物理引擎,force,gracity等),单位移动距离(speed)

 public abstract class MovingObject : MonoBehaviour
    {
        public float moveTime = 0.1f;           //Time it will take object to move, in seconds.
        public LayerMask blockingLayer;         //Layer on which collision will be checked.
        
        
        private BoxCollider2D boxCollider;      //The BoxCollider2D component attached to this object.
        private Rigidbody2D rb2D;               //The Rigidbody2D component attached to this object.
        private float inverseMoveTime;          //Used to make movement more efficient.

3.可重写的类Start

初始化包围盒和刚体

初始化单位移动时间

        //Protected, virtual functions can be overridden by inheriting classes.
        protected virtual void Start ()
        {
            //Get a component reference to this object's BoxCollider2D
            boxCollider = GetComponent <BoxCollider2D> ();
            
            //Get a component reference to this object's Rigidbody2D
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值