Drag and Drop 3D objects.
What I'm trying to do is first instantiate an object and then move it around the world in X and Z coordinates and on click of the 0 mouse button, leave the object there. The "Instantiate" function is called via a button. Everything seems to work, but when I move the "newObj" in +Z, the sphere starts jumping. Sometime when I click it doesn't respond and I have to do 2-3 clicks to set the position, is something wrong? public class DragDrop : MonoBehaviour { [SerializeField] GameObject sphere; GameObject newObj; Vector3 pos; bool isDrag;
void Start()
{
isDrag = false;
}
public void Instantiate()
{
RaycastHit hit1;
Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray1.origin, ray1.direction, out hit1))
{
isDrag = true;
newObj = Instantiate(sphere, hit1.point, Quaternion.identity);
pos = hit1.point;
}
}
void FixedUpdate()
{
if (isDrag == true)
{
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit2;
if (Physics.Raycast(ray2.origin, ray2.direction, out hit2, Mathf.Infinity))
{
newObj.transform.position = new Vector3(hit2.point.x, 0, hit2.point.z);
if(Input.GetMouseButtonDown(0))
{
newObj.transform.position = hit2.point;
isDrag = false;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
How do you set Prefabs into a GameObject array that will activate/deactivate when called? 1 Answer
How to creating a Gameobject from a prefab via scripted UI BUTTON 1 Answer
UnassignedReferenceException: The variable has not been assigned, even though it has. - c# 3 Answers
Changing multiple identical gameobjects into one prefab? 1 Answer
Scripted GameObjects vs Prefabs 0 Answers