我在运行时通过使用拖放操作创建了一个对象,我正在使用Input.getmousebutton
移动对象并调整对象的大小。在Unity中使用c#移动运行时创建的对象
当我尝试移动对象时,我按住鼠标按钮并且可以移动它,但是当我释放按钮并将光标移动到空白处然后单击鼠标按钮时,对象将移动到此处。
这是我的主要问题,但我也必须将相机移动到物体的缩放视图。
mycode的是:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ClickAction : MonoBehaviour {
//float distance=10;
public GameObject gm;
public string obj=null;
public Destroyer destroy;
//public MoveObject move;
private Vector3 screenPoint;
private Vector3 offset;
void Update()
{
if(Input.GetMouseButton(0))
{
if(gm)
{
gm.GetComponent<GameObject>();
gm.transform.position=new Vector3(Input.mousePosition.x,Input.mousePosition.y,1);
}
}
if (Input.GetMouseButtonDown (0))
{
Ray toMouse = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit rhInfo;
bool didHit = Physics.Raycast (toMouse, out rhInfo, 500.0f);
if (didHit)
{
gm=rhInfo.collider.gameObject;
Debug.Log (rhInfo.collider.name+"--"+gameObject.name);
obj=rhInfo.collider.name;
//move.OnMouseDown();
//move.OnMouseDrag();
//Calling Destroyer script to destroy object
destroy.getName(obj);
}
}
if (Input.GetMouseButton (1))
{
if (gm)
{
gm.GetComponent<GameObject>();
//gm.transform.localScale += new Vector3(0.3F, .3F, .3F);
gm.transform.localScale = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1);
}
}
}
}
'gm.GetComponent()'不会做任何事情,因为您没有将结果分配给任何东西。 –
Ben
我认为gm.GetComponent()无论如何都会返回null,因为组件继承了MonoBehaviour而不是GameObject –