带有空格的Unity3d绘图(对不起我的英文)

时间:2018-06-05 20:59:38

标签: c# unity3d game-physics

我使用LineRenderer绘制了线条。比赛是2d。现在,我想要的是将空格准确地放在游戏对象上的线与碰撞器的位置。不明白如何实现这一目标。对我来说这看起来很艰巨。 这是我用于绘制和添加对撞机的代码。我想要的不是在游戏对象上绘制线条,如圆圈(使用CircleCollider2D)或正方形(使用BoxCollider2D)。在这种情况下,我希望有两种不同的LineRenderers:一种是在对撞机上完成,第二种是在它上面开始。 What I'm trying to achieve那个方格不会隐藏这条线。广场没有线。

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

public class DrawLine : MonoBehaviour
{
    private LineRenderer line;
    private bool isMousePressed;
    public bool isLeftMousePressed = false;

    public List<Vector3> pointsList;

    private Vector3 mousePos;
    public float width = 0.05f;

    // Structure for line points
    struct myLine
    {
        public Vector3 StartPoint;
        public Vector3 EndPoint;
    };
    //    -----------------------------------    
    void Awake()
    {
        // Create line renderer component and set its property
        line = gameObject.AddComponent<LineRenderer>();
        line.material = (Material)Resources.Load("Materials/LineMat");

        line.startWidth = width;
        line.endWidth = width;

        line.useWorldSpace = true;
        isMousePressed = false;
        pointsList = new List<Vector3>();

    }
    //    -----------------------------------    
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
           isLeftMousePressed = true;
        }

        if (isLeftMousePressed||Game.currentLevel == 0) return;

        if (Input.GetMouseButton(0))
        {
            isMousePressed = true; 
        }

        // Drawing line when mouse is moving(pressed)
        if (isMousePressed)
        {
            if (Input.GetMouseButtonUp(0))
            {
                isMousePressed = false;

                GameObject curve = (GameObject)Instantiate(Resources.Load("Curve"), transform.parent);
                curve.GetComponent<DrawLine>().width = width;

                if (line.GetPosition(line.positionCount - 1).z == 1) Destroy(gameObject);
                else enabled = false;
            }

            mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            mousePos.z = 0;

            if (!pointsList.Contains(mousePos))
            {
                pointsList.Add(mousePos);
                line.positionCount = pointsList.Count;
                line.SetPosition(pointsList.Count - 1, (Vector3)pointsList[pointsList.Count - 1]);

                try
                {
                    AddColliderToLine(line, (Vector3)pointsList[pointsList.Count - 2], (Vector3)pointsList[pointsList.Count - 1]);
                }
                catch(Exception e)
                {

                }
            }   
        }
    }

    //    -----------------------------------    
    //    Following method checks whether given two points are same or not
    //    -----------------------------------    
    private bool checkPoints(Vector3 pointA, Vector3 pointB)
    {
        return (pointA.x == pointB.x && pointA.y == pointB.y);
    }

    private void AddColliderToLine(LineRenderer line, Vector3 startPoint, Vector3 endPoint)
    {
        //create the collider for the line
        GameObject lcObject = new GameObject("LineCollider");
        BoxCollider2D lineCollider = lcObject.AddComponent<BoxCollider2D>();

        lcObject.layer = 2; // ignore raycast

        //set the collider as a child of your line
        lineCollider.transform.parent = line.transform;
        // get width of collider from line 
        float lineWidth = line.endWidth;
        // get the length of the line using the Distance method
        float lineLength = Vector3.Distance(startPoint, endPoint);
        // size of collider is set where X is length of line, Y is width of line
        //z will be how far the collider reaches to the sky
        lineCollider.size = new Vector3(lineLength, lineWidth);
        // get the midPoint
        Vector3 midPoint = (startPoint + endPoint) / 2;

        // move the created collider to the midPoint
        lineCollider.transform.position = midPoint;


        //heres the beef of the function, Mathf.Atan2 wants the slope, be careful however because it wants it in a weird form
        //it will divide for you so just plug in your (y2-y1),(x2,x1)
        float angle = Mathf.Atan2((endPoint.y - startPoint.y), (endPoint.x - startPoint.x));

        // angle now holds our answer but it's in radians, we want degrees
        // Mathf.Rad2Deg is just a constant equal to 57.2958 that we multiply by to change radians to degrees
        angle *= Mathf.Rad2Deg;

        //were interested in the inverse so multiply by -1
        //angle *= -1;
        // now apply the rotation to the collider's transform, carful where you put the angle variable
        // in 3d space you don't wan't to rotate on your y axis
        lineCollider.transform.Rotate(0, 0, angle);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您想制作虚线,可以看到2D Dotted LineRenderer

在你想要的部分添加对撞机

 gameObject.AddComponent<PolygonCollider2D>();

修改

通过光线投射检查鼠标是否在广场内

if (isMousePressed)
{
    var stopDrawing = false;  
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit))
    {
        if(hit.transform.tag == "square" || hit.transform.tag == "circle") {
            stopDrawing  = true;
        }
    }
    if(stopDrawing) 
    {
        // Stop current line, prepare a new List<Vector3> for next line
    }
    else{
        // Continue draw current line
    }
}