C# UI映像正在错误位置实例化
我试着在屏幕上点击一条线,线的原点是我第一次点击的地方,线的终点是鼠标所在的地方,所有这些都发生在鼠标按下的时候。问题是,我给线条原点的值与鼠标在那一刻的值完全相同,但是被实例化了一个偏移量,我不明白为什么,因为我正在打印一些变换。线条原点的位置和鼠标的位置都匹配 脚本在面板中,由画布包含,画布渲染模式为世界空间 这里有一个屏幕截图,它精确地显示了我点击的位置和实例化的位置 代码:C# UI映像正在错误位置实例化,c#,unity3d,C#,Unity3d,我试着在屏幕上点击一条线,线的原点是我第一次点击的地方,线的终点是鼠标所在的地方,所有这些都发生在鼠标按下的时候。问题是,我给线条原点的值与鼠标在那一刻的值完全相同,但是被实例化了一个偏移量,我不明白为什么,因为我正在打印一些变换。线条原点的位置和鼠标的位置都匹配 脚本在面板中,由画布包含,画布渲染模式为世界空间 这里有一个屏幕截图,它精确地显示了我点击的位置和实例化的位置 代码: using System.Collections; using System.Collections.Gener
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour
{
public float mousePos;
public Camera cam;
public GameObject dirLine;
public Vector2 origin;
GameObject tempLine;
Vector2 ending;
Vector2 dir;
float width;
bool spawned;
// Start is called before the first frame update
void Start()
{
cam = GameObject.Find("Main Camera").GetComponent<Camera>();
spawned = false;
}
// Update is called once per frame
void Update()
{
//Debug.Log(Input.mousePosition);
if (tempLine != null)
{
ending = Input.mousePosition;
dir = ending - origin;
width = dir.magnitude;
//tempLine.GetComponent<RectTransform>().localPosition = origin;
tempLine.GetComponent<RectTransform>().sizeDelta = new Vector2(width, tempLine.GetComponent<RectTransform>().sizeDelta.y);
float angle = Vector2.SignedAngle(dir, Vector2.up);
tempLine.transform.eulerAngles = new Vector3(0, 0, -(angle - 90));
Debug.Log("LocalPosition de la linea:"+tempLine.GetComponent<RectTransform>().localPosition);
}
}
private void OnMouseDown()
{
spawned = true;
Vector3 screenpoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f);
//origin = GameObject.Find("Panel").transform.localPosition;
tempLine = Instantiate(dirLine,transform);
origin = screenpoint;
tempLine.transform.localPosition = origin;
tempLine.GetComponent<RectTransform>().localPosition = origin;
Debug.Log("Posicion del Mouse al ser clicado"+Input.mousePosition);
}
private void OnMouseUp()
{
spawned = false;
Destroy(tempLine);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类移动控制器:单行为
{
公众漂浮鼠标;
公共摄像机;
公共游戏对象目录;
公共向量2起源;
配子体模板;
向量2结束;
向量2-dir;
浮动宽度;
布尔产卵;
//在第一帧更新之前调用Start
void Start()
{
cam=GameObject.Find(“主摄像机”).GetComponent();
繁殖=假;
}
//每帧调用一次更新
无效更新()
{
//Debug.Log(Input.mousePosition);
if(模板行!=null)
{
结束=Input.mousePosition;
dir=结束-原点;
宽度=方向大小;
//tempLine.GetComponent().localPosition=原点;
tempLine.GetComponent().sizeDelta=新向量2(宽度,tempLine.GetComponent().sizeDelta.y);
浮动角度=矢量2.符号角度(方向,矢量2.向上);
tempLine.transform.eulerAngles=新矢量3(0,0,-(角度-90));
Log(“LocalPosition de la linea:+tempLine.GetComponent().LocalPosition”);
}
}
私有void OnMouseDown()
{
繁殖=真;
Vector3屏幕点=新Vector3(Input.mousePosition.x,Input.mousePosition.y,1.0f);
//origin=GameObject.Find(“Panel”).transform.localPosition;
tempLine=实例化(dirLine,transform);
原点=屏幕点;
tempLine.transform.localPosition=原点;
tempLine.GetComponent().localPosition=原点;
Log(“Posicion del Mouse al ser clicado”+Input.mousePosition);
}
私有void OnMouseUp()
{
繁殖=假;
销毁(模板);
}
}
屏幕截图显示了单击和实例化在同一地点发生-那么,为什么要在之后移动对象的localposition-如果不将其从单击的位置移开,您希望实现什么。你不是想移动你制作的模板项目的位置吗?请注意,当你更改LocalPosition时,其影响不是位置,因为你的刻度是0.3…我认为你的问题是你正在设置本地位置。您实例化它的位置是世界空间中的位置。。。。您可以使用这些坐标设置localPosition,它是相对于容器或父对象的位置。顺便说一下,您没有打印位置。。。您正在打印localPosition,它不是同一件事。鼠标位置不是本地的。因此,即使它与值匹配,原点也是不同的。我认为这里的关键是停止使用localposition,您似乎想要使用世界上的位置。