簡體   English   中英

如何圍繞特定的 object 和/或圍繞鼠標單擊 position 畫一個圓圈?

[英]How to draw a circle around specific object and/or around the mouse click position?

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

[ExecuteAlways]
[RequireComponent(typeof(UnityEngine.LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    [Range(1, 50)] public int segments = 50;
    [Range(1, 500)] public float xRadius = 5;
    [Range(1, 500)] public float yRadius = 5;
    [Range(0.1f, 5)] public float width = 0.1f;
    [Range(0, 100)] public float height = 0;
    public bool controlBothXradiusYradius = false;
    public bool draw = true;

    [SerializeField] private LayerMask targetLayers;
    [SerializeField] private LineRenderer line;

    private void Start()
    {
        if (!line) line = GetComponent<LineRenderer>();

        if (draw)
            CreatePoints();
    }

    private void Update()
    {
        if (Physics.CheckSphere(transform.position, xRadius, targetLayers))
        {
            Debug.Log("player detected");
        }
        else
        {
            Debug.Log("player NOT detected");
        }
    }

    public void CreatePoints()
    {
        line.enabled = true;
        line.widthMultiplier = width;
        line.useWorldSpace = false;
        line.widthMultiplier = width;
        line.positionCount = segments + 1;

        float x;
        float y;

        var angle = 20f;
        var points = new Vector3[segments + 1];

        for (int i = 0; i < segments + 1; i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
            y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;

            points[i] = new Vector3(x, height, y);

            angle += (380f / segments);
        }

        // it's way more efficient to do this in one go!
        line.SetPositions(points);
    }

#if UNITY_EDITOR
    private float prevXRadius, prevYRadius;
    private int prevSegments;
    private float prevWidth;
    private float prevHeight;

    private void OnValidate()
    {
        // Can't set up our line if the user hasn't connected it yet.
        if (!line) line = GetComponent<LineRenderer>();
        if (!line) return;

        if (!draw)
        {
            // instead simply disable the component
            line.enabled = false;
        }
        else
        {
            // Otherwise re-enable the component
            // This will simply re-use the previously created points
            line.enabled = true;

            if (xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments || width != prevWidth || height != prevHeight)
            {
                CreatePoints();

                // Cache our most recently used values.
                prevXRadius = xRadius;
                prevYRadius = yRadius;
                prevSegments = segments;
                prevWidth = width;
                prevHeight = height;
            }

            if (controlBothXradiusYradius)
            {
                yRadius = xRadius;

                CreatePoints();
            }
        }
    }
}
#endif

這將圍繞腳本附加到的 object 創建一個圓圈。

但是我想稍微改變一下腳本並添加一個目標變量,如果目標不是 null,即使腳本沒有附加到這個目標,也要圍繞目標轉一圈。 也許使用一個標志來決定是圍繞目標還是圍繞 object 創建圓,就像現在一樣。

我在頂部添加了一個目標變量:

public Transform target;

然后更改了 x 和 y 行:

x = target.position.x + Mathf.Cos(Mathf.Deg2Rad * angle) * xRadius;
y = target.position.y + Mathf.Sin(Mathf.Deg2Rad * angle) * yRadius;

但它不是在目標周圍繪制,當我為目標分配一個變換時,它只是在腳本附加到的變換周圍創建圓圈,而不是在目標周圍。

我正在嘗試做的另一件事是添加鼠標按下單擊事件,當鼠標按住並拖動時,它將在單擊的鼠標 position 周圍創建圓圈,並在按住鼠標並拖動鼠標的同時更改圓半徑.

但首先如何在目標周圍畫圓呢?

為了在目標周圍繪制圓圈,我嘗試在 Start() 中向目標 object 添加 LineRenderer 組件,但它沒有改變任何東西。

if(target.GetComponent<LineRenderer>() == null)
        {
            target.gameObject.AddComponent<LineRenderer>();
        }

你正在設置

line.useWorldSpace = false;

這意味着您提供的位置必須與此對象變換相關。

無論如何,這似乎不是您想要的,因此只需刪除該行或將其設置為

line.useWorldSpace = true;

或者,如果您確實想保留本地空間的功能,這意味着如果您移動此變換,那么該線將隨之移動-我對此表示懷疑,但只是為了完整性-您可以使用

points[i] = transform.InverseTransformPoint( new Vector3(x, height, y));

一個普遍的問題:為什么你的圓有380度而不是360度......?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM