编程语言
首页 > 编程语言> > Ruby's Adventure 08 添加网格陷阱 回存档点 敌人

Ruby's Adventure 08 添加网格陷阱 回存档点 敌人

作者:互联网

8.1 添加网格陷阱

8.2 当生命值降为0时重新开始游戏

8.3 添加敌人

添加网格陷阱

更改图片信息,使得能够做出网格陷阱

 改变Mesh Type ,Tight表示图像是一个完整的,不为多网格服务。将其改为Full Rect

 

 最后拖动能呈现出这样的功能,避免了形变

 

 添加Box Collider 2D,勾选Auto Tilling(这样碰撞范围就可以跟随拖动变化了),Is Trigger。

Is Trigger的作用是不将人物挡在陷阱外。

新建脚本DamageZone设置如下:

 

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class DamageZone : MonoBehaviour
 6 {
 7     private void OnTriggerStay2D(Collider2D collision)
 8     {
 9         PlayerContal contal = collision.GetComponent<PlayerContal>();
10         if (contal)
11         {
12             contal.ChangedHealth(-1);
13         }
14     }
15 }

 

OnTriggerZone2D

在PlayerController中设置如下:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class PlayerContal : MonoBehaviour
 6 {
 7     public float speed = 5f;
 8     //定义血量
 9     public int maxHealth = 5;
10     public int _currentHealth;
11     public float invicibletime = 2f;
12     private float invicibletimer;
13     private bool isinvicible = false;
14 
15     Rigidbody2D rbody;
16     private Animator _animator;
17 
18     private float _x;
19     private float _y;
20 
21     private Vector2 lookDir = Vector2.down;//向量向下
22     private Vector2 currentInput;
23 
24     public int Health => _currentHealth;
25     //返回当前血量
26 
27     private void Start()
28     {
29         rbody = GetComponent<Rigidbody2D>();
30         _animator = GetComponent<Animator>();
31         //初始化
32         _currentHealth = 1;//测试
33     }
34 
35     private void Update()
36     {
      ************************************** 37 if (isinvicible) 38 { 39 invicibletimer -= Time.deltaTime; 40 //减时间,如果时间<=0,就重置 41 if(invicibletimer <= 0) 42 { 43 isinvicible = false; 44 } 45 } 46     *************************************** 47 _x = Input.GetAxis("Horizontal"); 48 _y = Input.GetAxis("Vertical"); 49 50 Vector2 movement = new Vector2(_x, _y); 51 52 //动画部分,移动发生时,对朝向进行赋值 53 if(!Mathf.Approximately(movement.x, 0.0f) || !Mathf.Approximately(movement.y, 0.0f)) 54 { 55 lookDir.Set(movement.x, movement.y); 56 //对朝向进行归一化,更好的控制人物 57 lookDir.Normalize(); 58 } 59 //选择SetFloat和之间设置的浮点型进行对应 60 //lookx looky 就是之前设置的blend tree中的名称 61 _animator.SetFloat("lookx",lookDir.x); 62 _animator.SetFloat("looky",lookDir.y); 63 _animator.SetFloat("speed", movement.magnitude); 64 //magnitude(求模)是将数值变换限制在我们规定的范围内 65 66 currentInput = movement; 67 } 68 69 private void FixedUpdate() 70 { 71 Vector2 position = rbody.position; 72 position += currentInput * speed * Time.deltaTime; 73 rbody.MovePosition(position); 74 } 75 76 public void ChangedHealth(int amount) 77 {
      ************************************* 78 if(amount < 0) 79 { 80 if (isinvicible) return; 81 //如果掉血(Ture)就return 82 //否则就开始计时 83 isinvicible = true; 84 invicibletimer = invicibletime; 85 }
      ************************************* 86 _currentHealth = Mathf.Clamp(_currentHealth + amount, 0, maxHealth); 87 //现有血量,最小值,最大值 88 //对血量范围做出限制 89 print("Health" + _currentHealth); 90 } 91 }

 

 

8.2 当生命值降为0时重新开始游戏

新建空物体,命名为RespawnPosition,在脚本中进行如下设置:

添加变量 Public Transform respawnPosition;

 

private void Respawn()
{
     ChangedHealth(maxHealth);
     rbody.position = respawnPosition.position;
}

 将建立好的物品在unity里面将其关联起来。

 

 

这个方法的用途是恢复血量并将人物返回到初始点。在控制人物血量的方法里添加判断:

if(_currentHealth == 0) Respawn();

到这一步的代码如下(已折叠):

  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using UnityEngine;
  4 
  5 public class PlayerContal : MonoBehaviour
  6 {
  7     public float speed = 5f;
  8     //定义血量
  9     public int maxHealth = 5;
 10     public int _currentHealth;
 11     public Transform respawnPosition; //然后把rbody.position和它关联
 12 
 13     public float invicibletime = 2f;
 14     private float invicibletimer;
 15     private bool isinvicible = false;
 16 
 17     Rigidbody2D rbody;
 18     private Animator _animator;
 19 
 20     private float _x;
 21     private float _y;
 22 
 23     private Vector2 lookDir = Vector2.down;//向量向下
 24     private Vector2 currentInput;
 25 
 26     public int Health => _currentHealth;
 27     //返回当前血量
 28 
 29     private void Start()
 30     {
 31         rbody = GetComponent<Rigidbody2D>();
 32         _animator = GetComponent<Animator>();
 33         //初始化
 34         _currentHealth = 1;//测试
 35     }
 36 
 37     private void Update()
 38     {
 39         //如果掉血的话
 40         if (isinvicible)
 41         {
 42             invicibletimer -= Time.deltaTime;
 43             //减时间,如果时间<=0,就重置
 44             if(invicibletimer <= 0)
 45             {
 46                 isinvicible = false;
 47             }
 48         }
 49 
 50         _x = Input.GetAxis("Horizontal");
 51         _y = Input.GetAxis("Vertical");
 52 
 53         Vector2 movement = new Vector2(_x, _y);
 54 
 55         //动画部分,移动发生时,对朝向进行赋值
 56         if(!Mathf.Approximately(movement.x, 0.0f) ||  !Mathf.Approximately(movement.y, 0.0f))
 57         {
 58             lookDir.Set(movement.x, movement.y);
 59             //对朝向进行归一化,更好的控制人物
 60             lookDir.Normalize();
 61         }
 62         //选择SetFloat和之间设置的浮点型进行对应
 63         //lookx looky 就是之前设置的blend tree中的名称
 64         _animator.SetFloat("lookx",lookDir.x);
 65         _animator.SetFloat("looky",lookDir.y);
 66         _animator.SetFloat("speed", movement.magnitude);
 67         //magnitude(求模)是将数值变换限制在我们规定的范围内
 68 
 69         currentInput = movement;
 70     }
 71 
 72     private void FixedUpdate()  //控制人物移动速度,使得每台机器运行结果相同
 73     {
 74         Vector2 position = rbody.position;
 75         position += currentInput * speed * Time.deltaTime;
 76         rbody.MovePosition(position);
 77     }
 78 
 79     public void ChangedHealth(int amount)
 80     {
 81         if(amount < 0)
 82         {
 83             if (isinvicible) return;
 84             //如果掉血(Ture)就return
 85             //否则就开始计时
 86             isinvicible = true;
 87             invicibletimer = invicibletime;
 88         }
 89         _currentHealth = Mathf.Clamp(_currentHealth + amount, 0, maxHealth);
 90         //现有血量,最小值,最大值
 91         //对血量范围做出限制
 92         print("Health" + _currentHealth);
 93 
 94         if (_currentHealth == 0) Respawn();
 95     }
 96     //恢复满血
 97     private void Respawn()
 98     {
 99         ChangedHealth(maxHealth);
100         rbody.position = respawnPosition.position;
101     }
102  }
View Code

 

8.3 添加敌人

拖入敌人图片,添加刚体组件冻结Z轴并取消重力,添加胶囊Collider 2D,添加敌人的碰撞体积。

 选中enemy添加动画,与之前添加人物动画相同,不多叙述。

添加完成后进入enemy的动画控制器。

新建平衡树

进入平衡树,因为要对敌人进行上下左右四个方向的运动,即用x y两个变量来控制这个运动。

 

 blend type调整为2d

根据上左右下加入动画

添加并调整数值

 

 检查一下运动方向。

添加代码 控制敌人 能够出现一个左右的巡逻效果

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class EnemyController : MonoBehaviour
 6 {
 7     public float speed = 2f;
 8     //多少时间发生方向的反转
 9     public float timeToChangeDirection = 2f;
10     //判断方向的变量
11     public bool horizontal;
12 
13     //时间控制器
14     private float remainingTime;
15     //方向控制->初始方向是向右
16     private Vector2 _direction = Vector2.left;
17 
18     //刚体 动画控制
19     private Rigidbody2D _rigidBody2D;
20     private Animator _animator;
21 
22     //和平衡树中对应
23     private static readonly int lookx = Animator.StringToHash("lookx");
24     private static readonly int looky = Animator.StringToHash("looky");
25 
26     // Start is called before the first frame update
27     void Start()
28     {
29         _rigidBody2D = GetComponent<Rigidbody2D>();
30         _animator = GetComponent<Animator>();
31 
32         //时间初始化 timer
33         remainingTime = timeToChangeDirection;
34         //判断方向是否水平,并进行判断和更改
35         _direction = horizontal ? Vector2.left : Vector2.down;
36     }
37 
38     // Update is called once per frame
39     void Update()
40     {
41         //做倒计时
42         remainingTime -= Time.deltaTime;
43 
44         if (remainingTime <= 0)
45         {
46             remainingTime = timeToChangeDirection;
47             //****改变方向****
48             _direction *= -1;
49         }
50 
51         _animator.SetFloat(lookx, _direction.x);
52         _animator.SetFloat(looky, _direction.y);
53 
54 
55     }
56 
57     //对刚体的操作
58     private void FixedUpdate()
59     {
60         _rigidBody2D.MovePosition(_rigidBody2D.position + _direction * speed * Time.deltaTime);
61     }
62 }

选择horizontal后就可以让其横向运动了。

 

 将enemy加入预置器就能大量产出敌人了(雾

增加敌人的碰撞检测

 1 private void OnCollisionEnter2D(Collision2D collision)
 2     {
 3         PlayerContal controller = collision.collider.GetComponent<PlayerContal>();
 4 
 5         //判断人物控制是否为空,如果不为空,则执行掉血的操作
 6         if(controller)
 7         {
 8             controller.ChangedHealth(-1);
 9         }
10     }

*美化

 添加

 

 调整层、颜色、形状、方向、透明度

 

标签:08,float,private,添加,Adventure,currentHealth,using,Ruby,public
来源: https://www.cnblogs.com/OnlyACry/p/13696611.html