当前位置:网站首页>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
边栏推荐
- Basic introduction to pod
- Greenplus enterprise deployment
- Compare three clock circuit schemes of single chip microcomputer
- Solve the problem that the disk is full due to large files
- Record of problems caused by PIP upgrade damage
- Dynamic thresholds buffer management in a shared buffer packet switch paper summary
- My entrepreneurial neighbors
- 智慧城市的应用挑战,昇腾AI给出了新解法
- Ionicons icon Encyclopedia
- Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI
猜你喜欢

Day 014 二维数组练习

An Optimal Buffer Management Scheme with Dynamic Thresholds论文总结

Jianmu continuous integration platform v2.5.2 release

Actual measurement of boot and pH pins of buck circuit
![[WPF] realize language switching through dynamic / static resources](/img/23/1e089ce4a07128323824b25897a8c4.png)
[WPF] realize language switching through dynamic / static resources

C language data type

Measured waveform of boot capacitor short circuit and open circuit of buck circuit

MySQL uses date_ FORMAT(date,'%Y-%m')

Amaze UI icon query

Zero technology is deeply involved in the development of privacy computing financial scenario standards of the ICT Institute
随机推荐
Mutationobserver document learning
[memo] summary of the reasons why SSH failed? Remember to come next time.
Blue Bridge Cup group a selection XOR
The beauty of do end usage
Vmstat memory consumption query
关于pip升级损坏导致的问题记录
[FPGA tutorial case 42] image case 2 - realize image binarization processing through Verilog, and conduct auxiliary verification through MATLAB
Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention
Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI
postman接口测试|js脚本之阻塞休眠和非阻塞休眠
Some thoughts on growing into an architect
Tcp/ip five layer reference model and corresponding typical devices and IPv6
C language data type
Mqtt server setup and mqtt.fx testing
黑盒测试常见错误类型说明及解决方法有哪些?
Android interview question | how to write a good and fast log library?
In JS, 0 means false, and non-0 means true
[skill accumulation] common expressions when writing emails
Amaze UI icon query
flutter只要是数据,都会判空的