Unity3D 2D游戏开发 官方教程。(七)
2015-12-29 10:04
393 查看
七移动脚本
7.1创建脚本
创建移动脚本MovingObject.cs控制对象的移动,此类为Player以及Enemy移动控制的几类。
7.2 修改脚本
using UnityEngine; using System.Collections; public abstract class MovingObject : MonoBehaviour { //对象移动所需时间 public float moveTime = 0.1f; //碰撞层,所有需要检测碰撞的对象都在此层中 public LayerMask blockingLayer; //碰撞检测 private BoxCollider2D boxCollider; //钢体组件 private Rigidbody2D rb2D; //高效计算使用 private float inverseMoveTime; protected virtual void Start () { //获取当前对象的组件信息 boxCollider = GetComponent<BoxCollider2D>(); rb2D = GetComponent<Rigidbody2D>(); //乘法比除法效率高 inverseMoveTime = 1f/ moveTime; } //移动并检测碰撞 protected bool Move(int xDir,int yDir,out RaycastHit2D hit) { Vector2 start = transform.position; Vector2 end = start + new Vector2(xDir,yDir); //停止自身碰撞器,防止射线碰撞到自己的碰撞器 boxCollider.enabled = false; //发射射线从start到end,并检测BlockingLayer中的碰撞情况。如果有碰撞返回到hit中 hit = Physics2D.Linecast(start,end,blockingLayer); boxCollider.enabled = true; if(hit.transform == null) { //hit为空,表示没有碰撞对象,即可以移动 StartCoroutine(SmoothMovement(end)); return true; } return false; } //T为要检测的目标碰撞对象类型wall,food,enemy,exit等 protected virtual void AttemptMove<T>(int xDir,int yDir) where T : Component { RaycastHit2D hit; bool canMove = Move (xDir,yDir,out hit); if(hit.transform == null) return; T hitComponent = hit.transform.GetComponent<T>(); if(!canMove && hitComponent != null) OnCantMove(hitComponent); } //对象平滑移动 protected IEnumerator SmoothMovement(Vector3 end) { //计算到end点距离 float sqrRemainingDistance = (transform.position - end).sqrMagnitude; //平滑移动(float.Epsilon无线接近0) while(sqrRemainingDistance > float.Epsilon) { Vector3 newPosition = Vector3.MoveTowards(rb2D.position , end,inverseMoveTime * Time.deltaTime); rb2D.MovePosition(newPosition); sqrRemainingDistance = (transform.position - end).sqrMagnitude; yield return null; } } //路线上有碰撞对象不能移动的时候,需要发生的控制 protected abstract void OnCantMove<T>(T component) where T : Component; }
相关文章推荐
- Unity3D 2D游戏开发 官方教程。(十三)
- Unity3D 2D游戏开发 官方教程。(十四)
- Unity3D 2D游戏开发 官方教程。(八)
- Unity3D 2D游戏开发 官方教程。(十五)
- Unity3D 2D游戏开发 官方教程。(十一)
- Unity3D 2D游戏开发 官方教程。(十二)
- Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第二节(下)
- Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- 工具设置Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- 工具设置Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第二节(上)
- Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- Unity3D官方二维游戏开发教程中文版分享
- Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第三节
- Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- 选中拖动Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第二节(下)
- Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第二节(下)
- Unity3d Platformer Pro 2D游戏开发框架使用教程
- 【Unity3D】学习笔记(第3记) 2D游戏开发基本技巧之场景切换(Javascript)