美文网首页Unity技术分享
Unity3d制作流星拖尾效果

Unity3d制作流星拖尾效果

作者: 小黑Unity_齐xc | 来源:发表于2018-09-04 19:18 被阅读5次
a.gif

1.拖尾效果

1.1 新建空物体,命名为star
1.2 新建子空物体,命名为trail;
1.3 trail对象上添加TrailRenderder组件,如下图:


trail1.jpeg

1.4 点击color,编辑颜色


trail2.jpeg

2.流星的移动

通过刚体rigidbody2d来控制
2.1 选中star对象,添加rigidbody2d组件
2.2 选择body type 为Kinematic(运动学),具体请看https://www.jianshu.com/p/004c3e00aeec
2.3 我们通过代码设置rigidbody.velocity的值来控制其运动,创建脚本star

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class star : MonoBehaviour {

    Rigidbody2D rigidbody;

    public float x = -5;
    public float y = -1;

    Camera cam;
    float size = -1;

    void Awake(){
        rigidbody = this.GetComponent<Rigidbody2D> ();
        rigidbody.velocity = new Vector2 (x,y);

        cam = Camera.main;
        float height = 2f * cam.orthographicSize;
        size = height;
    }

    void OnEnable()
    {
        StopAllCoroutines();
        StartCoroutine(CoUpdate());
    }

    IEnumerator CoUpdate()
    {
        while(true)
        {
            if(IsBehind())
            {
                break;
            }
            yield return new WaitForSeconds(1);;
        }
        StopCoUpdate();
    }

    void StopCoUpdate()
    {
        GameObject.Destroy (gameObject);
        StopAllCoroutines();
    }

    bool IsBehind()
    {
        //判断是否超出屏幕一定距离
        float distance = Vector2.Distance(transform.position, cam.transform.position);
        if (distance > size / 2f) {
            return true;
        }
        return false;
    }
}

2.4 运行游戏,star物体就会根据物理速率自动运行,拖尾效果也随之出现,一定距离后,会自动销毁该star。

3. 通过预制体实现批量流星效果,如文章顶部的图片

3.1 使用star创建预制体
3.2 使用脚本动态创建star,随机指定初始的位置
3.3 创建脚本starsManager,拖拽给相机对象,并为脚本指定预制体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class starsManager : MonoBehaviour {
    public GameObject pre;

    float time = 0.5f;
    float timer = 0;

    void Update () {
        timer += Time.deltaTime;
        if(timer<time){
            return;
        }
        timer = 0;
        time = Random.Range (0, 5) * 0.3f;

        GameObject go = GameObject.Instantiate (pre);
        go.transform.position = new Vector2 (random(),random());
    }

    private float random(){
        return (Random.Range(0, 4) * 0.7f)+4f;
    }
}

运行,即可出现文章顶部的流星效果了。

相关文章

  • Unity3d制作流星拖尾效果

    1.拖尾效果 1.1 新建空物体,命名为star1.2 新建子空物体,命名为trail;1.3 trail对象上添...

  • 复习之刀光

    条带数据,制作拖尾。 刀光绑到剑上,拖尾两个插槽。 贴花,MF-texture,通过mask在特定区域实现流动效果...

  • Unity 粒子特效—FX Maker

    FX Maker是Unity3d一款非常流行的效果制作插件。不但有超过300种效果预制体, 还可以自己制作效果。包...

  • ASE Shader 实现拖尾效果

    附上源码: 参考地址:(RzFX Flow Trail in Unity - YouTube[https://ww...

  • iTween笔记

    1.OrbitExample 1.1 Trail Renderer 拖尾效果http://www.ceeger.c...

  • PS教程:浪漫流星雨

    本章内容主要是针对“滤镜风格化”的应用,从而制作出浪漫流星雨效果,制作动态流星效果则需要用到时间轴,图文有点长,建...

  • cocos2dx拖尾效果

  • 粒子效果

    特效组件(靠材质体现): component-effect 拖尾效果: 镜头光晕(Lens Flars):涉及到后...

  • 月经拖尾

    月经拖尾食方分享:例假后期如果有拖尾现象,用红糖水煮1个鸡蛋+7粒白果(去皮,去芯)每天一次,例假收得很快,2次见效。

  • 天空在吃棉花糖

    狂风挂过,湛蓝的天滑过了一颗流星,慧尾拖闪拖闪的掩映在蓝天之中,像一根针般穿插在蓝色的这块牛仔布之上,就像你破旧...

网友评论

    本文标题:Unity3d制作流星拖尾效果

    本文链接:https://www.haomeiwen.com/subject/cmsnwftx.html