简体   繁体   English

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

[英]Unity - how do i move the pencil higher then the mouse

The problem is that the pencil it's way too close to the mousePosition. 问题在于铅笔离mousePosition太近了。 i want to put the pencil higher than the mousePosition. 我想把铅笔比mousePosition高。 here is an image as reference : https://ibb.co/s6v25t9 这是参考图像: https : //ibb.co/s6v25t9

The thing i want to achieve, it's the pencil to be higher than the line drawn 我要实现的目标是铅笔比画的线高

This is taken from a project. 这取自一个项目。 it's not my own code but i am trying to figure out how to fix some bugs and this one i cant really figure out how to fix 这不是我自己的代码,但我正在尝试找出如何修复某些错误,而我却无法真正找出解决方法

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);                              


}

In the code you posted the value of LastMosPos is never changed so I can't say where you get it from. 在您发布的代码中, LastMosPos的值从未更改,因此我无法说出它的来源。

However instead of the line 然而,而不是线

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

you probably could simply add a certain offset like eg 您可能只需添加一个特定的偏移量,例如

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

to make the pencil appear 20 Unity units (in a Screenspace Overlay canvas this euqals 20 Pixels) above the LastMosPos . 使铅笔出现20统一单位(在屏幕空间覆盖画布此euqals 20个像素)以上LastMosPos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM