hello ,
i want to drag an object on another and i am using raycast for the it…
but following code is not working as programmed it do not print the log messages as one object collide with the object using raycast…
please help me to find the error…
using UnityEngine;
using System.Collections;
public class Drag_drop : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
{
RaycastHit hit = new RaycastHit();
Ray ray = new Ray();
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
Debug.Log("pew pew");
if (hit.collider.gameObject.name.Equals("Cube"))
{
Debug.Log("go brooks");
// BrooksCallOut.gameObject.SendMessage("gotoBrooks");
}
}
//
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}