当前位置:网站首页>Unity初学3——敌人的移动控制和掉血区域的设置(2d)
Unity初学3——敌人的移动控制和掉血区域的设置(2d)
2022-07-29 05:24:00 【林浮生】
该文来自学习chutianbo老师的笔记,链接b站
在我们制作好地图和回复道具之后,我们就需要为地图加上一个敌人。
敌人的简易移动1
public class EmenyController : MonoBehaviour
{
//设定移动速度变量
public float speed = 0.1f;
//声明一个2d刚体对象
Rigidbody2D rigidbody2D;
// 声明 Vector2 对象来存放敌人当前位置
Vector2 position;
//声明一个初始 y 坐标变量
float initY;
//声明一个移动方向的变量
float direction;
//存放移动距离的变量,设置为共有,开放在 unity 中的访问
public float distance = 4;
void Start()
{
// 获取这些对象或变量在游戏开始时的值
rigidbody2D = GetComponent<Rigidbody2D>();
//获取起始位置
position = transform.position;
//获取起始y坐标
initY = position.y;
//设定初始移动方向
direction = 1.0f;
}
// Update is called once per frame
private void FixedUpdate()
{
//通过刚体移动的方法调用,放入 fixupdate方法中,0.02秒执行一次
MovePosition();
}
private void MovePosition()
{
if (position.y - initY < distance && direction > 0)
{
position.y += speed;
}
if (position.y - initY >= distance && direction > 0)
{
direction = -1.0f;
}
if (position.y - initY > 0 && direction < 0)
{
position.y -= speed;
}
if (position.y - initY <= 0 && direction < 0)
{
direction = 1.0f;
}
rigidbody2D.position = position;
}
}
这段代码开放了移动速度,通过判定和初始位置的距离来进行来回移动(仅仅设定了在y轴上);当然x轴上改下代码就可以
第二种移动控制(来自官网)
public class EnemyController2 : MonoBehaviour
{
public float speed;
public bool vertical;
Rigidbody2D rigidbodyroot;
//朝一个方向走的时间
public float changeTime = 3.0f;
//计时器
float timer;
//方向
int direction=1;
void Start()
{
rigidbodyroot = GetComponent<Rigidbody2D>();
timer = changeTime;
}
private void Update()
{
timer -= Time.deltaTime;
if (timer < 0)
{
direction = -direction;
timer = changeTime;
}
}
private void FixedUpdate()
{
Vector2 position = rigidbodyroot.position;
if (vertical)
{
position.y = position.y + speed * Time.deltaTime*direction;
}
else
{
position.x = position.x + speed * Time.deltaTime * direction;
}
rigidbodyroot.MovePosition(position);
}
//刚体碰撞事件
private void OnCollisionEnter2D(Collision2D other)
{
RubyController rubyController = other.gameObject.GetComponent<RubyController>();
if (rubyController != null)
{
rubyController.changehealth(-1);
}
}
}
这段代码开放了折返时间和速度以及行走方向;通过行走时间来控制行走距离;
同时加入了机器人碰到主角ruby时使主角掉血;
加入掉血区域
在掉血区域我们使用钉子作为图片素材,因为我们要实现在其中要一直收到判定,而不是只在碰到触发器时判定
所以代码中我们使用 OnTriggerStay2D 表示每一帧都会触发触发器
RubyController rubyController = collision.GetComponent<RubyController>();
if (rubyController != null)
{
rubyController.changehealth(DamageNum);
}
问题1:关于这个区域需要较大时,我们的素材会重复使用,这里我们就需要用到平铺素材
平铺素材第一件事,我们需要回到素材界面在sprite Mode下找到Mesh Type使用Full Rect
然后在在预制件中的Box Collider 2D中勾选Auto Tiling即可。
问题2:判定使用的代码
unity中自带三种判定 OnTriggerEnter2D OnTriggerStay2D OnTriggerExit2D分别表示进入时判定一次
在触发器内每帧判定一次和离开时判定一次;
这片文章所使用的素材来自unity商店Ruby’s adventure
链接unity官网
边栏推荐
- 从头安装MYSQL(MYSQL安装文档-解压版)
- leetcode---技巧
- 滑动窗口 Leetcode 76.最小覆盖子串(困难) 76.76. MinimumWindow Substring (Hard)
- JUC并发知识点
- Eight sorts ------------- heap sort
- 给二维表添加时间序列索引
- Understanding of synchronized eight lock phenomenon
- 八大排序----------------冒泡排序
- Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图
- 官方教程 Redshift 03 各种GI的参数和常规使用说明
猜你喜欢
UE4 天光和反射球的原理和区别
Leetcode 977. Square of ordered array
给二维表添加时间序列索引
【Leetcode刷题】数组2——二分查找
LeetCode #189.轮转数组
FPGA based: multi-target motion detection (hand-in-hand teaching ①)
官方教程 Redshift 03 各种GI的参数和常规使用说明
LeetCode #7.整数反转
ML10 self study notes SVM
[beauty of software engineering - column notes] 13 | how to break the rhythm of writing code during daytime meetings and overtime?
随机推荐
LeetCode #876.链表的中间结点
网络爬虫
Shell tool finalshell
scanBasePackages扫包范围配置
文件系统一
简洁代码实现pdf转word文档
Mathematical modeling experience
Simple code to realize PDF to word document
leetcode刷题笔记 605. Can Place Flowers (Easy) 605.种花问题
LeetCode #19.删除链表的倒数第N个结点
Leetcode 876. Intermediate node of linked list
Leetcode 557. reverse word III in string
【Leetcode刷题】数组3——分治
从头安装MYSQL(MYSQL安装文档-解压版)
LeetCode #7.整数反转
寒假集训总结 (1.23~1.28) [第一梯队]
Leetcode 977. Square of ordered array
Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)
#7110 数字走向2 题解
leetcode---技巧