unity制作手雷抛物线

这篇文章主要是介绍制作流程,读者可以根据自己所需修改代码,附有代码注释和全部代码;

第一步,在hierarchy面板创建一个Line物体,用于后面抛物线的渲染。

 第二步,属性Inspector面板修改属性;可以修改抛物线的宽度,Materials下一步制作;

 

 第三步,制作透明的抛物线材质parabola;新建材质,编写透明材质所需着色器;

 1处选择所写的透明着色器parabola代码,全部代码如下,2处修改填充方式,3处可以更改透明度;注:也可以选择系统自带的着色器;

Shader "Unlit/parabola"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Quene"="Transparent"}
        LOD 100
        Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv+float2(_Time.y,0));
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

第四步,开始抛物线的制作,根据确定发出点,初始速度,发出方向,每隔deltaTime计算抛物线上的点,然后把点位置储存在数组中,把数组传入绘制抛物线Line Rendererd的方法中;具体代码如下:

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

public class ParabolaControl : MonoBehaviour
{
    private bool IsDown;
    private float Delay = 0.1f;                                         //延迟相当于按下持续时间
    private float LastDownTime = 1;//
    public GameObject point;                                         //设置一个抛出位置前方的点,可以通过改变这个点的位置,改变抛出初速度方向;
    public LineRenderer parabola;                                //抛物线
    public GameObject circle;                                       //抛物线末端小球
    public float timespan;                                                 //计算抛物线点的时间间隔
    public float speed;                                                       //手雷初速度标量
    public GameObject grenade;                                           //手雷预制体
    private GameObject player;                                           //玩家
    private Animator Anim;                                                    //玩家动画


    // Start is called before the first frame update
    void Start()
    {
        
        player = GameObject.FindGameObjectWithTag("Player");         //获取玩家
        Anim = player.GetComponent<Animator>();              //玩家动画
    }

    // Update is called once per frame
    void Update()
    {
        transform.forward = point.transform.position - gameObject.transform.position;//初速度方向
        if (IsDown)                              //判断手雷按钮按下后,绘制抛物线等操作
        {
            if (Time.time - LastDownTime >= Delay)                //是否为长按操作
            {
                circle.SetActive(true);                               //抛物线末端小球
                parabola.enabled = true;                         //持续画线
                List<Vector3> list = GetVector3s();              //线点集合
                parabola.positionCount = list.Count;
                parabola.SetPositions(list.ToArray());      //设置线点列表位置传入抛物线绘制方法
                circle.transform.position = list[list.Count - 1];    //抛物线末端最后一个点被挡住后画小球
                LastDownTime = Time.time;
            }
        }

    }
    
    public void buttondown()                                    //手雷按钮按下
    {
        IsDown = true;                                            //按钮标志为按下
        LastDownTime = Time.time;                       //持续时间
    }

    public void buttonup()                              //手雷按钮抬起事件
    {
        
        Debug.Log("雷按钮抬起");
        parabola.enabled = false;                         //取消手雷抛物线
        Anim.SetBool("Grenade", true);                  //玩家开火动画
        GameObject grenadeinstance = Instantiate(grenade, transform.position, transform.rotation); //生成手雷预制体
        grenadeinstance.GetComponent<Rigidbody>().velocity = speed * transform.forward;             //手雷的初速度矢量为速度和抛出点正方向的乘积
        Destroy(grenadeinstance, 3f);           //三秒后销毁手雷预制体

        circle.SetActive(false);
        IsDown = false;
    }

    List<Vector3> GetVector3s()
    {
        List<Vector3> list = new List<Vector3>();
        Vector3 horizontalDir = new Vector3(transform.forward.x, 0, transform.forward.z).normalized;//水平方向
        float angle = 360 - this.transform.rotation.eulerAngles.x;//与水平方向夹角
        float horizontalSpeed = Mathf.Cos(angle / 180 * Mathf.PI) * speed;//水平方向速度
        float verticalSpeed = Mathf.Sin(angle / 180 * Mathf.PI) * speed;//垂直方向速度
        for (int i = 0; i<1000 ; i++)//点数量上限为1000个
        {
            //点位置为初始位置加速度乘以时间间隔,方向向上
            Vector3 position = transform.position + (horizontalSpeed * timespan * i * horizontalDir)+ ((verticalSpeed + (verticalSpeed + Physics.gravity.y * timespan * i)) / 2 * timespan * i * transform.up);
            //某时刻抛物线点位置,
            list.Add(position);
            if(i > 0)//点大于两个,用于计算末尾方向rearDir
            {
                RaycastHit hit;
                Vector3 rearDir = list[list.Count - 1] - list[list.Count - 2];//最后一段被障碍物挡住的线段方向
                if(Physics.Raycast(list[list.Count - 2],rearDir,out hit,rearDir.magnitude))//
                {
                    list[list.Count - 1] = hit.point;                                  //最后一个点设为射线碰撞点
                    break;//结束
                }
            }
        }
        return list;//返回列表
    }
}

第四步:将此脚本ParabolaControl挂载在抛出点上;

 最后附上效果图:

猜你喜欢

转载自blog.csdn.net/qq_57282862/article/details/125726285
今日推荐