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