当前位置:网站首页>Unity beginner 3 - enemy movement control and setting of blood loss area (2D)
Unity beginner 3 - enemy movement control and setting of blood loss area (2D)
2022-07-29 07:57:00 【Lin Fusheng】
This article comes from learning chutianbo Teacher's notes , link b standing
After we make the map and reply to the props , We need to add an enemy to the map .
Easy movement of the enemy 1
public class EmenyController : MonoBehaviour
{
// Set the movement speed variable
public float speed = 0.1f;
// Make a statement 2d Rigid objects
Rigidbody2D rigidbody2D;
// Statement Vector2 Object to store the current position of the enemy
Vector2 position;
// Declare an initial y Coordinate variables
float initY;
// Declare a variable for the direction of movement
float direction;
// Store the variable of moving distance , Set to common , Opening up in unity Visit in
public float distance = 4;
void Start()
{
// Get the values of these objects or variables at the beginning of the game
rigidbody2D = GetComponent<Rigidbody2D>();
// Get the starting position
position = transform.position;
// Get start y coordinate
initY = position.y;
// Set the initial moving direction
direction = 1.0f;
}
// Update is called once per frame
private void FixedUpdate()
{
// Method call through rigid body movement , Put in fixupdate In the method ,0.02 Once per second
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;
}
}
This code opens up the mobile speed , Move back and forth by determining the distance from the initial position ( Only set in y On the shaft ); Of course x Just change the code on the axis
The second movement control ( From official website )
public class EnemyController2 : MonoBehaviour
{
public float speed;
public bool vertical;
Rigidbody2D rigidbodyroot;
// Time to go in one direction
public float changeTime = 3.0f;
// timer
float timer;
// Direction
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);
}
// Rigid body collision event
private void OnCollisionEnter2D(Collision2D other)
{
RubyController rubyController = other.gameObject.GetComponent<RubyController>();
if (rubyController != null)
{
rubyController.changehealth(-1);
}
}
}
This code opens the turn back time and speed as well as the walking direction ; Control the walking distance by walking time ;
At the same time, the robot meets the protagonist ruby Time makes the protagonist lose blood ;
Add blood loss area

In the area of blood loss, we use nails as picture material , Because we want to realize that we should always receive judgment , Instead of just deciding when you encounter a trigger
So we use OnTriggerStay2D It means that each frame will trigger a trigger
RubyController rubyController = collision.GetComponent<RubyController>();
if (rubyController != null)
{
rubyController.changehealth(DamageNum);
}
problem 1: When this area needs to be larger , Our material will be reused , Here we need to use tile material
The first thing about tiling materials , We need to go back to the material interface in sprite Mode Find below Mesh Type Use Full Rect
Then in the prefabricated part Box Collider 2D The admission Auto Tiling that will do .
problem 2: Determine the code used
unity There are three kinds of judgments in OnTriggerEnter2D OnTriggerStay2D OnTriggerExit2D It means to judge once when entering
It is determined once per frame in the trigger and once when leaving ;
The material used in this article comes from unity The store Ruby’s adventure
link unity Official website
边栏推荐
- MySQL 45 | 08 is the transaction isolated or not?
- My entrepreneurial neighbors
- In JS, 0 means false, and non-0 means true
- [note] the art of research - (tell a good story and argument)
- 你学习·我奖励,21天学习挑战赛 | 等你来战
- Strongly connected component
- Popular cow G
- Very practical shell and shellcheck
- Solving linear programming problems based on MATLAB
- Zip gzip tar compression Advanced Edition
猜你喜欢
功能自动化测试实施的原则以及方法有哪些?

IonIcons图标大全

Do you want to meet all the needs of customers

Chaos and future of domestic digital collections

Zero technology is deeply involved in the development of privacy computing financial scenario standards of the ICT Institute

Jump from mapper interface to mapping file XML in idea
![[freeze electron microscope] analysis of the source code of the subtomogram alignment function of relion4.0 (for self use)](/img/fe/0efdd151f9661d5cd06a79b7266754.png)
[freeze electron microscope] analysis of the source code of the subtomogram alignment function of relion4.0 (for self use)

Keyboard processing in jetpack compose

Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)

Monitor the bottom button of page scrolling position positioning (including the solution that page initialization positioning does not take effect on mouse sliding)
随机推荐
Convert source package to RPM package
MySQL uses date_ FORMAT(date,'%Y-%m')
UPC little C's King Canyon
The database uses PSQL and JDBC to connect remotely and disconnect automatically from time to time
Do you want to meet all the needs of customers
Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
In the MySQL connector of flynk CDC, the MySQL field is varbinary, which is officially
Up sampling deconvolution operation
Research on autojs wechat: the final product of wechat automatic information sending robot (effective demonstration)
[密码学实验] 0x00 安装NTL库
MySQL 45 | 08 is the transaction isolated or not?
Data warehouse modeling, what is wide table? How to design? Advantages and disadvantages
C language data type
Volatile keyword parsing of C #
[cryoelectron microscope | paper reading] emclarity: software for high-resolution cryoelectron tomography and sub fault averaging
MySQL 45 讲 | 07 行锁功过:怎么减少行锁对性能的影响?
C# 之 volatile关键字解析
Detailed explanation of the find command (the most common operation of operation and maintenance at the end of the article)
Output 1234 three digits without repetition
Solving linear programming problems based on MATLAB