2d平台怪物逻辑
2d来回巡逻
遇到坑会自动转向
可配置单次方向行走的时间,转向等待时间等
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Serialization; [RequireComponent(typeof(Rigidbody2D))]
public class AI2dPatrol : MonoBehaviour
{
public Rigidbody2D body2D;
public Transform rayPoint;//射线发射点
public float rayLength = ;//射线长度
public float speed = ;//怪物速度
public int turnWaitTime;//转向时间
public float oneTime;//走一个方向的时间
public bool autoTurn = true;//遇坑自动转向 private float nowTime = ;
private int moveDir = ;//移动方向 1为左
public Vector3 curSpeed;
private bool isPause = false; private void FixedUpdate()
{
if (isPause)
{
return;
} Move();
if (oneTime > )
{
if (nowTime >= oneTime)
{
nowTime = ;
TurnFace();
}
nowTime += Time.deltaTime;
}
if (autoTurn)
{
RaycastHit2D groundInfo = Physics2D.Raycast(rayPoint.position, Vector2.down, rayLength);
if (groundInfo.collider == false)
{
TurnFace();
}
//scene模式下显示射线
Debug.DrawRay(rayPoint.position, Vector2.down * rayLength, Color.blue);
}
} //转向
async void TurnFace()
{
isPause = true;
await Task.Delay(turnWaitTime * );
moveDir = -moveDir;
var origin = transform.localScale;
transform.localScale = new Vector3(-origin.x, origin.y, origin.z);
isPause = false;
} //移动
void Move()
{
//transform.Translate(Vector2.left * speed * Time.deltaTime * moveDir);
Vector3 targetVelocity = new Vector2(-moveDir * speed, body2D.velocity.y);
body2D.velocity = Vector3.SmoothDamp(body2D.velocity, targetVelocity, ref curSpeed, 0.1f);
}
}
2d平台怪物逻辑的更多相关文章
- HTML5 2D平台游戏开发#6地图绘制
此前已经完成了一部分角色的动作,现在还缺少可以交互的地图让游戏看起来能玩.不过在开始之前应当考虑清楚使用什么类型的地图,就2D平台游戏来说,一般有两种类型的地图,Tile-based和Art-base ...
- HTML5 2D平台游戏开发#4状态机
在实现了<HTML5 2D平台游戏开发——角色动作篇之冲刺>之后,我发现随着角色动作的增加,代码中的逻辑判断越来越多,铺天盖地的if() else()语句实在让我捉襟见肘: 这还仅仅是角色 ...
- HTML5 2D平台游戏开发#5攻击
目前为止,角色除了基本的移动外还什么都不能做,于是我打算先实现角色的攻击动画.角色的普通攻击一共可以分为三个阶段: 一段斩 二段斩 三段斩 移动攻击 跳跃攻击 触发方式为角色站立时按下J(攻击)键,角 ...
- HTML5 2D平台游戏开发#11斜坡物理
在游戏中会经常遇到斜坡地形,比如众所周知的魂斗罗,角色可以在坡上移动和跳跃: 斜坡在2D游戏中很常见,处理起来也较为棘手.最初我打算用分离轴定律来实现,在建立了一个物理模型之后: 发现上坡时没什么问题 ...
- HTML5 2D平台游戏开发#8指令技
一般在动作游戏中,玩家可以通过对输入设备输入一系列的指令让角色完成某个或多个特定的动作.以格斗游戏<拳皇>为例,键入↓↘→↘↓↙← + A or C可以触发IORI的必杀技八稚女: 通过一 ...
- HTML5 2D平台游戏开发#7Camera
在庞大的游戏世界中,玩家不能一览地图全貌,而是只能看到其中一部分,并一步步探索,这时就要用到一种技术来显示局部的地图,游戏术语称为摄像机(Camera).下面两张图中的白色矩形框表示了Camera的作 ...
- HTML5 2D平台游戏开发#1
在Web领域通常会用到一组sprite来展示动画,这类动画从开始到结束往往不会有用户参与,即用户很少会用控制器(例如鼠标.键盘.手柄.操作杆等输入设备)进行操作.但在游戏领域,sprite动画与控制器 ...
- web 单一平台登录逻辑
单点登录逻辑token=cookie('login_token');serverToken=get(token);if(serverToken!='注销'){ set('userid_已登录token ...
- (转载)TRS内容管理平台用户注册逻辑漏洞
首先 site:gov.cn inurl:WCM TRS 的内容管理系统是国内政府网站使用最多的系统之一 如上面所说:外交部 http://wcm.fmprc.gov.cn/wcm/ 网址加上:wcm ...
随机推荐
- uboot dcc
arch\arm\lib crt0.S 1.设置sp为CONFIG_SYS_INIT_SP_ADDR include/configs/xxx.h #define CONFIG_SYS_INIT_SP_ ...
- vue项目配置及项目初识
目录 Vue项目环境搭建 Vue项目创建 重构项目依赖 1.需要转移的文件 2.重构依赖 pycharm配置并启动vue项目 vue项目目录结构分析 vue组件(.vue文件) 全局脚本文件main. ...
- 【leetcode】Valid Parenthesis String
题目: Given a string containing only three types of characters: '(', ')' and '*', write a function to ...
- 安装c#服务
https://www.cnblogs.com/zmztya/p/9577440.html 1.以管理员身份运行cmd 2.安装windows服务 cd C:\Windows\Microsoft.NE ...
- 记录下:nth-child在table中遇到的问题~(已解决)
首先做了一个表格,如下: <!DOCTYPE html> <html> <head> <title></title> <style t ...
- 模板_SEG_TREE
#include<bits/stdc++.h> #define maxn 500005 #define int long long using namespace std; inline ...
- 2019hdu多校 Keen On Everything But Triangle
Problem Description N sticks are arranged in a row, and their lengths are a1,a2,...,aN. There are Q ...
- IoC容器简介
官网说明: 在核心的第7章节 1 Spring IoC 容器和 beans 简介 本章介绍了 Spring Framework implementation of the Inversion of C ...
- ModelSerializer 使用知识点_serializer.save(project=obj) #外键一定要作为实例传入save函数,否则无法新增成功
1.有两个模型如下 A.project class Project(models.Model): """ 项目表 """ id = mode ...
- 将DVD.iso 挂载到虚拟机
将DVD.iso挂载到虚机之后,默认设备名是/dev/sr0 挂载DVD.iso文件到/mnt. 操作: mount /dev/sr0 /mnt 查看: ll /mnt