> 文章列表 > 【unity learn】【Ruby 2D】角色发射飞弹

【unity learn】【Ruby 2D】角色发射飞弹

【unity learn】【Ruby 2D】角色发射飞弹

前面制作了敌人的随机运动以及动画控制,接下来就是Ruby和Robot之间的对决了!

世界观背景下,小镇上的机器人出了故障,致使全镇陷入了危机,而Ruby肩负着拯救小镇的职责,于是她踏上了修复机器人的旅途。

之前其实一直挺好奇的,fps是怎么样发射子弹的呢?

现在我终于明白了,方法就是

先设置我们的飞弹零件预制体

首先创建一个预制体

将图片拖到Hierarchy窗口再拖到prefabs文件夹就可以做到了,然后我们进行一些基本的设置

 

添加碰撞体和刚体组件,使其能够与机器人发生碰撞

然后我们应该设置脚本了

脚本的内容有三点

1、Awake生命周期内需要获取这个刚体组件(不用start是因为在你创建对象时 Unity 不会运行 Start,而是在下一帧才开始运行。因此,在飞弹上调用 Launch 时,只实例化 (Instantiate),不调用 Start,因此 Rigidbody2d 仍然为空。

2、Launch发射方法:使用了AddForce方法,为其施加一个方向与力的乘积(不得不说做游戏物理很重要)

3、发生撞击删除自己还有飞行一定距离后删除自己

下面是飞弹的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Myprojectile : MonoBehaviour
{Rigidbody2D rigidbody2d;// Start is called before the first frame updatevoid Awake(){rigidbody2d = GetComponent<Rigidbody2D>();//获取飞弹刚体组件}// Update is called once per framevoid Update(){if (transform.position.magnitude > 1000.0f)//向量长度大于1000销毁该飞弹{Destroy(gameObject);}}public void Launch(Vector2 direction,float force){rigidbody2d.AddForce(direction * force);//施加一个力}void OnCollisionEnter2D(Collision2D other)//这一段是击中了机器人后的代码,fix在Robot控制里面,下文会提到{MyEnemyController e = other.collider.GetComponent<MyEnemyController>();获取机器人对象if(e != null)//没撞到机器人{e.Fix();调用撞击}Destroy(gameObject);//发生撞击后删除自己}}

 接下来是机器人被击中后的代码

不太多

主要是建立一个bool 变量broken

然后在每个生命周期内都添加判断条件

if (!broken){return;}

然后就是fix函数

public void Fix(){broken = false;//被击中Rd.simulated = false;//将rigidbody2d的这个属性关闭以后,刚体会从物理系统中移除animator.SetTrigger("Fixed");//这个是被打中后跳舞的动作smokeEffect.Stop();//这个是关闭粒子特效,我们下文再讲解}

完整代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MyEnemyController : MonoBehaviour
{public float Speed = 0.1f;public bool vertical;public float changeTime = 3.0f;Rigidbody2D Rd;float timer;int direction = 1;private float _timer = 0f;private Animator animator;public bool broken = true;public ParticleSystem smokeEffect;// Start is called before the first frame updatevoid Start(){Rd = GetComponent<Rigidbody2D>();timer = changeTime;animator = this.GetComponent<Animator>();//动画控制}// Update is called once per frameprivate void Update(){if (!broken)//被击中{return;}timer -= Time.deltaTime;if(timer < 0){direction = -direction;timer = changeTime;}}void FixedUpdate()//这一段主要是运动,看不懂可以翻我之前的博客{if (!broken)//判断,如果是被击中后的状态,就直接跳出{return;}float dt = Time.fixedDeltaTime;_timer -= dt;if (_timer < 0){_timer = Random.Range(0.3f, 2.0f);vertical = !vertical;}Vector2 position = Rd.position;if (vertical){position.y = position.y + Time.deltaTime * Speed * direction;animator.SetFloat("MoveX", 0);animator.SetFloat("MoveY", direction);}else{position.x = position.x + Time.deltaTime * Speed * direction;animator.SetFloat("MoveX",direction );animator.SetFloat("MoveY",0 );}Rd.MovePosition(position);//MovePosition:导入一个vector向量来修改当前position位置}void OnCollisionEnter2D(Collision2D other){MyRubyController player = other.gameObject.GetComponent<MyRubyController>();if (player != null){player.ChangeHealth(-1);}}public void Fix()//被击中{broken = false;Rd.simulated = false;animator.SetTrigger("Fixed");smokeEffect.Stop();}
}

险些离题,最后我们来写Ruby发射飞弹的代码

这里我们会使用Instantiate方法

参数的话,或许我们看代码会更加易懂

void Launch(){GameObject projectileObject = Instantiate(projectilePrefab/*我们要创建的预制体*/, rb2d.position//创建初始位置 + Vector2.up *0.5f//离初始位置有多远, Quaternion.identity//这里表示不旋转);Myprojectile projectile = projectileObject.GetComponent<Myprojectile>();//获取飞弹projectile.Launch(lookDirection, 300);//为飞弹添加力animator.SetTrigger("Launch");//播放Ruby攻击动画}

我们Instantiate方法这里是不是会报错呢,报错就对了,因为我们还没有获取飞弹组件

这里给出的方法是创建GameObject公共变量,然后让飞弹挂载在Ruby身上

操作如下

public GameObject projectilePrefab;

这样,我们就能够发射飞弹了。

成果演示如下