Unity-如何将铅笔移到比鼠标高的位置

时间:2019-05-15 10:57:02

标签: c# unity3d

问题在于铅笔距离mousePosition太近了。我想把铅笔比mousePosition高。 这是参考图片:https://ibb.co/s6v25t9

我要实现的目标是,铅笔要高于画线

这是从项目中提取的。这不是我自己的代码,但我正在尝试找出如何修复一些错误,而我却无法真正找出解决方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.IO;


public class GameManager : MonoBehaviour
{

    [Tooltip("The color of the drawn lines")]
    public Color lineColor;
    public Material lineMaterial;
    public Transform Pencil;
    public Sprite SurpriseGlass;
    public Sprite HappyGlass;
    public Sprite SadGlass;
    public Slider PenCapacity;
    public Text PenPercent;
    [HideInInspector]
    public GameObject[] Hint;

    public Image Star1;
    public Image Star2;
    public Image Star3;
    public GameObject LevComp;
    private GameObject[] Obs;
    private List<GameObject> listLine = new List<GameObject>();
    public List<Vector2> listPoint = new List<Vector2>();
    private GameObject currentLine;
    public GameObject currentColliderObject;
    private GameObject hintTemp;
    private GameObject[] waterTap;
    private GameObject Glass;
    private Vector3 LastMosPos;
    private BoxCollider2D currentBoxCollider2D;
    private LineRenderer lines;
    private LineRenderer currentLineRenderer;
    private bool stopHolding;
    private bool allowDrawing = true;
    [HideInInspector]
    public bool completed;
    int clickCont;
    private List<Rigidbody2D> listObstacleNonKinematic = new List<Rigidbody2D>();
    private GameObject[] obstacles;
    float mosDis;
    bool canCreate;
    RaycastHit2D hit_1;
    RaycastHit2D hit_2;
    RaycastHit2D hit_3;
    GameObject TemLine;
    void Start()
    {
        Pencil.gameObject.SetActive(false);
        waterTap = GameObject.FindGameObjectsWithTag("Interactive");
        Glass = GameObject.FindGameObjectWithTag("GlassParent");
        Glass.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
        Hint = GameObject.FindGameObjectsWithTag("Hint");
        for (int i = 0; i < Hint.Length; i++)
        {
            Hint[i].SetActive(false);
        }
        lineMaterial.SetColor("_Color", lineColor);
        Obs = GameObject.FindGameObjectsWithTag("Obstacle");
        for (int i = 0; i < Obs.Length; i++)
        {
            Obs[i].GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
        }
    }

    void Update()
    {
        if (PenCapacity.value <= 0.01f || !Input.GetMouseButton(0))
        {
            Pencil.gameObject.SetActive(false);
        }

        if (Input.GetMouseButtonDown(0))
        {
            GameObject thisButton = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;     //Get the button on click
            if (thisButton != null)                                                                             //Is click on button
            {
                allowDrawing = false;
                print("cant drwa");
            }
            else                                                                                                //Not click on button
            {
                allowDrawing = true;
                stopHolding = false;
                listPoint.Clear();
                CreateLine(Input.mousePosition);
                print("draw");
            }
        }
        else if (Input.GetMouseButton(0) && !stopHolding && allowDrawing && PenCapacity.value > 0)
        {
            RaycastHit2D rayHit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (LastMosPos != Camera.main.ScreenToWorldPoint(Input.mousePosition))
            {
                if (rayHit.collider == null)
                {

                    Pencil.gameObject.SetActive(true);
                    Pencil.position = new Vector3(LastMosPos.x, LastMosPos.y, 0);
                    if (canCreate == false)
                    {
                        float dist = Vector3.Distance(LastMosPos, Camera.main.ScreenToWorldPoint(Input.mousePosition));
                        Pencil.GetComponent<TrignometricRotation>().enabled = true;
                        PenCapacity.value = PenCapacity.value - dist / 25;
                        PenPercent.text = Mathf.FloorToInt(PenCapacity.value * 100).ToString() + " %";
                        if (Mathf.FloorToInt(PenCapacity.value * 100) < 75)
                        {
                            Star3.gameObject.SetActive(false);
                        }
                        if (Mathf.FloorToInt(PenCapacity.value * 100) < 50)
                        {
                            Star2.gameObject.SetActive(false);
                        }
                        if (Mathf.FloorToInt(PenCapacity.value * 100) < 25)
                        {
                            Star1.gameObject.SetActive(false);
                        }
                    }
                }
            }
            else
            {
                Pencil.GetComponent<TrignometricRotation>().enabled = false;
            }

                Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                float ab = Vector2.Distance(LastMosPos, mousePos);
                mosDis = mosDis + ab;

            if (!listPoint.Contains(mousePos) && mosDis > .02f)
            {
                mosDis = 0;
                //Add mouse pos, set vertex and position for line renderer
                if (canCreate == false)
                {
                    if (rayHit.collider == null)
                    {
                        listPoint.Add(mousePos);                              


}

1 个答案:

答案 0 :(得分:2)

在您发布的代码中,LastMosPos的值从未更改,因此我无法说出它的来源。

但是不是行

Pencil.position = new Vector3(LastMosPos.x, LastMosPos.y, 0);

您可能只需添加一个特定的偏移量,例如

Pencil.position = new Vector3(LastMosPos.x, LastMosPos.y + 20f, 0);

使铅笔出现在20上方LastMosPos单位单位(在屏幕空间覆盖画布中,该像素为20像素)。