C# 统一-如何将铅笔移动到高于鼠标的位置

C# 统一-如何将铅笔移动到高于鼠标的位置,c#,unity3d,C#,Unity3d,问题是铅笔离鼠标位置太近了。我想把铅笔放得比鼠标位置高。 以下是一张图片作为参考: 我想要达到的目标是,铅笔要比画的线高 这是取自一个项目。这不是我自己的代码,但我正试图找出如何修复一些bug,而这一个我真的不知道如何修复 using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine.UI; using System.IO; pu

问题是铅笔离鼠标位置太近了。我想把铅笔放得比鼠标位置高。 以下是一张图片作为参考:

我想要达到的目标是,铅笔要比画的线高

这是取自一个项目。这不是我自己的代码,但我正试图找出如何修复一些bug,而这一个我真的不知道如何修复

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


}

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine.SceneManagement;
使用UnityEngine.UI;
使用System.IO;
公共类GameManager:MonoBehavior
{
[工具提示(“绘制线的颜色”)]
公共颜色线颜色;
公共材料线条材料;
公共转换铅笔;
公共雪碧玻璃;
公共精神快乐玻璃;
公共雪碧玻璃;
公共能力;
公共文本百分比;
[hideininstecpt]
公共游戏对象[]提示;
公众形象明星1;
公众形象之星2;
公众形象之星3;
公共游戏对象LevComp;
私有游戏对象[]Obs;
私有列表listLine=新列表();
public List listPoint=新列表();
私有游戏对象线;
公共游戏对象;
私有游戏对象hintTemp;
私人游戏对象[]水龙头;
私人游戏物体玻璃;
私有向量3 LastMosPos;
专用BoxCollider2D currentBoxCollider2D;
专用线路;
私有线条渲染器;
私人银行停止持有;
私有布尔allowDrawing=true;
[hideininstecpt]
公共图书馆建成;
int clickCont;
私有列表ListObstaclenOnKinetic=新列表();
私人游戏对象[]障碍;
浮动mosDis;
布尔癌症;
RaycastHit2D命中率为1;
RaycastHit2D hit_2;
RaycastHit2D hit_3;
配子体淡线;
void Start()
{
Pencil.gameObject.SetActive(false);
waterTap=GameObject.FindGameObjectsWithTag(“交互式”);
Glass=GameObject.FindGameObjectWithTag(“GlassParent”);
Glass.GetComponent().bodyType=RigidByType2D.Static;
Hint=GameObject.FindGameObjectsWithTag(“提示”);
for(int i=0;i0.02f)
{
mosDis=0;
//添加鼠标位置,设置线条渲染器的顶点和位置
if(canCreate==false)
{
if(rayHit.collider==null)
{
添加(鼠标点);
}

在您发布的代码中,
LastMosPos
的值从未更改,因此我无法说明您从何处获得它

然而,不是线

Pencil.position = new Vector3(LastMosPos.x, LastMosPos.y, 0);
您可以简单地添加某个偏移量,例如

Pencil.position = new Vector3(LastMosPos.x, LastMosPos.y + 20f, 0);
要使铅笔显示在
LastMosPos
上方的
20
Unity单位(在屏幕空间覆盖画布中,此euqals为20像素)