Unity3d如何从相机上的鼠标位置开始线

时间:2018-05-01 00:00:51

标签: c# unity3d

目前,该线从我点击并锁定到那些坐标开始 但是,我希望线条从我点击屏幕开始,直到我放开鼠标才开始移动 (游戏目前有一个球落下)。

using UnityEngine;
using System.Collections;
public class TrailCollider : MonoBehaviour 
{
    private LineRenderer line; // Reference to LineRenderer
    private Vector3 mousePos;    
    public PhysicsMaterial2D phys;
    private Vector3 startPos;    // Start position of line
    private Vector3 endPos;    // End position of line
    void Update () 
    {

        // On mouse down new line will be created 
        if(Input.GetMouseButtonDown(0))
        {
            if(line == null)
                createLine();

            mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            mousePos.z = 0;
            line.SetPosition(0,mousePos);
            line.SetPosition(1,mousePos);
            startPos = mousePos;

        }
        else if(Input.GetMouseButtonUp(0))
        {
            if(line)
            {
                mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                mousePos.z = 0;
                line.SetPosition(1,mousePos);
                endPos = mousePos;
                addColliderToLine();
                line = null;
            }
        }
        else if(Input.GetMouseButton(0))
        {
            if(line)
            {
                mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                mousePos.z = 0;
                line.SetPosition(1,mousePos);
            }
        }
    }
    // Following method creates line runtime using Line Renderer component
    private void createLine()
    {
        line = new GameObject("Line").AddComponent<LineRenderer>();
        line.material =  new Material(Shader.Find("Diffuse"));
        line.SetVertexCount(2);
        line.SetWidth(0.1f,0.1f);
        line.SetColors(Color.black, Color.black);
        line.useWorldSpace = true;    
    }
    // Following method adds collider to created line
    private void addColliderToLine()
    {
        BoxCollider2D col = new GameObject("Collider").AddComponent<BoxCollider2D> ();
        col.transform.parent = line.transform; // Collider is added as child object of line
        col.sharedMaterial = phys;
        float lineLength = Vector3.Distance (startPos, endPos); // length of line
        col.size = new Vector3 (lineLength, 0.1f, 1f); // size of collider is set where X is length of line, Y is width of line, Z will be set as per requirement
        Vector3 midPoint = (startPos + endPos)/2;
        col.transform.position = midPoint; // setting position of collider object
        // Following lines calculate the angle between startPos and endPos
        float angle = (Mathf.Abs (startPos.y - endPos.y) / Mathf.Abs (startPos.x - endPos.x));
        if((startPos.y<endPos.y && startPos.x>endPos.x) || (endPos.y<startPos.y && endPos.x>startPos.x))
        {
            angle*=-1;
        }
        angle = Mathf.Rad2Deg * Mathf.Atan (angle);
        col.transform.Rotate (0, 0, angle);
    }
}

0 个答案:

没有答案