• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Unity3D_物体抛物线运动

武飞扬头像
笔记罐头
帮助1

学新通

参考:Unity 贝塞尔曲线实现抛物线运动,投掷功能实现 

用到了DOTween插件, 回想起之前不知道这个插件还在用协程和SmoothDamp做运动控制的自己仿佛是个哈批


代码:

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using DG.Tweening;
  5.  
     
  6.  
    public class Test : MonoBehaviour
  7.  
    {
  8.  
    private Vector3 _startPoint = new Vector3(0, 0, 0);//设置原点为起点
  9.  
    private float _lineHeight;//抛物线高度
  10.  
     
  11.  
    public GameObject 炮弹预制体;//抛射物体模型
  12.  
    private GameObject _bullet;
  13.  
     
  14.  
    public LineRenderer lineRender;
  15.  
     
  16.  
    private bool _moveFinish = true;
  17.  
     
  18.  
    void Start()
  19.  
    {
  20.  
    _bullet = Instantiate(炮弹预制体);
  21.  
    _bullet.SetActive(false);
  22.  
    }
  23.  
     
  24.  
    void Update()
  25.  
    {
  26.  
    //从摄像机发出到鼠标位置方向的射线
  27.  
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  28.  
    RaycastHit hit;
  29.  
    LayerMask mask = 1 << (LayerMask.NameToLayer("Floor"));//射线只检测"Floor"图层
  30.  
     
  31.  
    if (Physics.Raycast(ray, out hit,Mathf.Infinity,mask.value))
  32.  
    {
  33.  
    GameObject gameobj = hit.collider.gameObject;
  34.  
     
  35.  
    float Dis = Vector3.Distance(_startPoint, hit.point);
  36.  
     
  37.  
    _lineHeight = Dis/2;//随距离变化曲率
  38.  
     
  39.  
    /*绘制抛物线*/
  40.  
    var bezierControlPoint = (_startPoint hit.point) * 0.5f (Vector3.up*_lineHeight);
  41.  
     
  42.  
    int resolution = 50;//曲线上的路径点数量,值越大,取得的路径点越多,曲线越平滑
  43.  
     
  44.  
    var _path = new Vector3[resolution];
  45.  
    for (int i = 0; i < resolution; i )
  46.  
    {
  47.  
    var t = (i 1) / (float)resolution;//归化到0~1范围
  48.  
    _path[i] = GetBezierPoint(t,_startPoint,bezierControlPoint,hit.point);//使用贝塞尔曲线的公式取得t时的路径点
  49.  
    }
  50.  
     
  51.  
    lineRender.positionCount = _path.Length;
  52.  
    lineRender.SetPositions(_path);
  53.  
     
  54.  
    if(Input.GetMouseButtonDown(0))
  55.  
    {
  56.  
    if (_moveFinish)
  57.  
    {
  58.  
    _bullet.transform.position = _startPoint;
  59.  
    _bullet.SetActive(true);
  60.  
    _bullet.transform.DOPath(_path, 1f).SetEase(Ease.OutSine).OnComplete(MoveCallback);
  61.  
    _moveFinish = false;
  62.  
    }
  63.  
    }
  64.  
    }
  65.  
    }
  66.  
     
  67.  
    void MoveCallback()
  68.  
    {
  69.  
    _moveFinish = true;
  70.  
    }
  71.  
     
  72.  
    /// <param name="t">0到1的值,0获取曲线的起点,1获得曲线的终点</param>
  73.  
    /// <param name="start">曲线的起始位置</param>
  74.  
    /// <param name="center">决定曲线形状的控制点</param>
  75.  
    /// <param name="end">曲线的终点</param>
  76.  
    public static Vector3 GetBezierPoint(float t, Vector3 start, Vector3 center, Vector3 end)
  77.  
    {
  78.  
    return (1 - t) * (1 - t) * start 2 * t * (1 - t) * center t * t * end;
  79.  
    }
  80.  
    }
学新通

路径绘制使用的LineRenderer

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgafkaa
系列文章
更多 icon
同类精品
更多 icon
继续加载