手指在屏幕上滑動快速形成牆體、柵欄等直線型建築

以2D爲例,三維的你自己改一下。把下面的腳本放在一個空物體上,然後把預製拖入進去,我的預製體碰撞器使用的是環形碰撞器CircleCollider2D。半徑的25倍就是兩個柵欄之間的距離。然後添加一個線性渲染組件模擬拖拽軌跡。

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

public class InitFence : MonoBehaviour {

    public GameObject _Box;
    public float _Dis;

    private LineRenderer _LineRenderer;
    private Vector3 _StartPos;
    private Vector3 _EndPos;
    private Vector3 _TempPos;
    private Vector3 m_v3;

    private int num = 0;

    private bool _PressDown = false;

    private void Start()
    {
        //獲取生成物體之間的距離
        _Dis = _Box.GetComponent<CircleCollider2D>().radius * 2;
        _LineRenderer = GetComponent<LineRenderer>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //初始化線性渲染點
            _LineRenderer.positionCount = 2;
            _StartPos =  ScreentoWorldPos(Input.mousePosition);
            _PressDown = true;
        }


        if (Input.GetMouseButtonUp(0))
        {
            //結束軌跡渲染
            _LineRenderer.positionCount = 0;
            _PressDown = false;
            _EndPos = ScreentoWorldPos(Input.mousePosition);
            float leng = Vector3.Distance(_StartPos, _EndPos);

            num = Mathf.CeilToInt(leng / _Dis);

            float angle = this.angle(_EndPos, _StartPos);
          
            for (int i = 0; i < num; i++)
            {
                GameObject go = Instantiate(_Box) as GameObject;
                go.transform.position =_StartPos + new Vector3((_EndPos.x - _StartPos.x) * (i ) / num, (_EndPos.y - _StartPos.y) * (i ) / num, 0);
                go.transform.rotation = Quaternion.Euler(0, 0, angle);

            }
        }

        //畫出拖拽的軌跡
        if (_PressDown)
        {
            _TempPos = ScreentoWorldPos(Input.mousePosition);  
            _LineRenderer.SetPositions(new Vector3[2] { _StartPos, _TempPos });
        }

    }

    /// <summary>
    /// 屏幕座標轉換成世界座標
    /// </summary>
    /// <param name="inputPos"></param>
    /// <returns></returns>
    Vector3 ScreentoWorldPos(Vector3 inputPos)
    {
        m_v3 = inputPos;
        m_v3.z -= Camera.main.transform.position.z;
        m_v3 = Camera.main.ScreenToWorldPoint(m_v3);
        return m_v3;
    }

    /// <summary>
    /// 轉換角度
    /// </summary>
    /// <param name="from"></param>
    /// <param name="to"></param>
    /// <returns></returns>
    float angle(Vector3 from, Vector3 to)
    {
        //兩點的x、y值
        float x = from.x - to.x;
        float y = from.y - to.y;

        //斜邊長度
        float hypotenuse = Mathf.Sqrt(Mathf.Pow(x, 2f) + Mathf.Pow(y, 2f));

        //求出弧度
        float cos = x / hypotenuse;
        float radian = Mathf.Acos(cos);

        //用弧度算出角度    
        float angle = 180 / (Mathf.PI / radian);

        if (y < 0)
        {
            angle = -angle;
        }
        else if ((y == 0) && (x < 0))
        {
            angle = 180;
        }
        return angle;
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章