I have solve it
How to create a prefab and then drag it by mouse
Hi , thank you for looking through this question. Now I want to init a gameobject which packed into a prefab on the position where I click and then drag it with my cursor. Because the object will be destoryed when I release the left mousebutton, so once I press the left mousebutton I'll init the gameobject and if I want to drag the button I can't release the mousebutton.
But now I can't let the gameobject go with the cursor , the following scripts have any mistakes to make it not work ?
Here is the script.NO1, this script will init a gameobject on the position where I clicked
using UnityEngine;
using System.Collections;
public class CreateObjectAtPoint : MonoBehaviour
{
public Transform HelpObj;
public GameObject clone;
void Start()
{
}
public void FixedUpdate()
{
if(Input.GetMouseButtonDown(0))
{
//Debug.Log("click");
Plane targetPlane = new Plane(Vector3.forward, 4.0f);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance = 0;
targetPlane.Raycast(ray, out distance);
Vector3 clonePos = ray.GetPoint(distance);
// Debug.Log("x: " + clonePos.x + " y: " + clonePos.y + " z: " + clonePos.z);
clone = Instantiate(HelpObj, clonePos, Quaternion.identity) as GameObject;
}
}
}
And there is script NO2, this scripts will destory the object I init when I release the left mousebutton and make the object go with cursor before I release the button:
using UnityEngine;
using DG.Tweening;
using System.Collections;
public class FingerMove : MonoBehaviour
{
private Plane m_TargetPlane;
private float m_swipeSpeed = 0.1f;
private bool isMove = true;
public static float inputX;
public static float inputY;
void Awake()
{
}
void Update()
{
if(Input.GetMouseButtonUp(0))
{
Destroy(this.gameObject);
}
}
IEnumerator OnMouseDown()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane p = new Plane(Vector3.forward, 4.0f);
float distance = 0;
p.Raycast(ray, out distance);
while (Input.GetMouseButton(0))
{
Debug.Log("X: " + ray.GetPoint(distance).x + " Y: " + ray.GetPoint(distance).y + " Z:" + ray.GetPoint(distance).z);
this.gameObject.transform.position = ray.GetPoint(distance);
yield return new WaitForFixedUpdate();
}
}
}