【unity Learn】【Ruby 2D】キャラクターがミサイルを発射する

敵のランダムな動きとアニメーション制御は以前に行われ、次のステップはルビーとロボットの決闘です!

その世界観を背景に、町のロボットが故障して町全体が危機に瀕し、町を救う責任を負ったルビーは、ロボットを修理する旅に出る。

私は実際に前に非常に興味がありました.fpsはどのように弾丸を発射しますか?

今ようやく理解しました、方法は

まず、ミサイル パーツのプレハブをセットアップします

まずはプレハブを作成

画像を Hierarchy ウィンドウにドラッグしてから prefabs フォルダーにドラッグして実行し、基本的な設定を行います。

 

Collider コンポーネントと Rigidbody コンポーネントを追加して、ロボットと衝突できるようにします

次に、スクリプトを設定する必要があります

脚本の内容は3点

1. Awake ライフ サイクル中にこの剛体コンポーネントを取得する必要があります (Unity はオブジェクトの作成時に Start を実行せず、次のフレームで実行を開始するため、開始する必要はありません。したがって、Launch を呼び出すときにミサイル、インスタンス化のみ (Instantiate )、Start は呼び出されないため、Rigidbody2d は空のままです。

2. Launch 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 update
    void Awake()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();//获取飞弹刚体组件
    }

    // Update is called once per frame
    void 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変数を作成する

その後、ライフサイクルごとに判定条件を追加

if (!broken)
        {
            return;
        }

次に、修正機能があります

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 update
    void Start()
    {
        Rd = GetComponent<Rigidbody2D>();
        timer = changeTime;
        animator = this.GetComponent<Animator>();//动画控制
    }

    // Update is called once per frame
    private 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 public 変数を作成し、Ruby にミサイルをマウントさせることです。

操作は次のとおりです。

public GameObject projectilePrefab;

このようにして、ミサイルを発射できます。

結果は次のように示されます。

 

 

おすすめ

転載: blog.csdn.net/qq_63499305/article/details/130081269
おすすめ