无法在屏幕上保持精灵,光线投射选择所有的碰撞boader和移动他们,整个移动回地面
问题描述:
我有一个精灵通过点击和拖动移动和代码工作正常,直到我试图包含精灵离开现场。这个问题是我无法弄清楚如何拖动一个游戏对象并忽略场景的其余部分。尝试使用if语句和标签,但不能将字符串转换为布尔或其他转换错误。下面无法在屏幕上保持精灵,光线投射选择所有的碰撞boader和移动他们,整个移动回地面
码被附加到主摄像机
public GameObject gameObjectToDrag; // refer to Go that being dragged
public Vector3 Gocenter; // gameobject centre
public Vector3 touchPosition; // touch or click position
public Vector3 offSet; // vector between touchpoint/mouse click to the object centre
public Vector3 newGOCenter; // new center of object
RaycastHit hit; // store hit object information
public bool draggingmode = false; //
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update()
{
//********************************
// **Click to Drag****************
//********************************
如果UNITY_EDITOR
// first frame when user click left button
/* if (Input.GetMouseButton(0))
{
// convert mouse position to ray
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if ray hit a collider (not 2dcollider)
if (Physics.Raycast(ray, out hit))
{
gameObjectToDrag = hit.collider.gameObject;
Gocenter = gameObjectToDrag.transform.position;
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
offSet = touchPosition - Gocenter;
draggingmode = true;
}
}
// every frame when user hold left mouse
if (Input.GetMouseButton(0))
{
if (draggingmode)
{
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
newGOCenter = touchPosition - offSet;
gameObjectToDrag.transform.position = new Vector3(newGOCenter.x, newGOCenter.y, newGOCenter.z);
}
}
*/
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit2d = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (Physics.Raycast(ray, out hit))
{
gameObjectToDrag = hit.collider.gameObject;
Gocenter = gameObjectToDrag.transform.position;
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
offSet = touchPosition - Gocenter;
draggingmode = true;
}
if (hit2d)
{
Debug.Log("Hit something: " + hit2d.transform.tag);
gameObjectToDrag = hit2d.collider.gameObject;
Gocenter = gameObjectToDrag.transform.position;
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
offSet = touchPosition - Gocenter;
draggingmode = true;
}
if (Input.GetMouseButton(0))
{
if (draggingmode)
{
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
newGOCenter = touchPosition - offSet;
gameObjectToDrag.transform.position = new Vector3(newGOCenter.x, newGOCenter.y, newGOCenter.z);
}
}
}
if (Input.GetMouseButtonUp(0))
{
draggingmode = false;
}
ENDIF
答
这是我所使用的层忽略光线投射简单。
对不起