Plane is blocked all Raycast
I use Ray to find game characters, and a Coordinate Plane for moving these characters. But when the Plane is turned on, it blocks all Ray. There may be alternatives.
P.s. Sorry for English
public class Grab : MonoBehaviour {
public Plane plane;
public LayerMask HitMaskChar;
public LayerMask HitMaskClone;
public Vector3 posPlane = new Vector3 (0f, 0f, -5f);
public float distance = 10f;
private bool isClone = false;
private GameObject clone;
// Use this for initialization
void Start () {
plane = new Plane (Vector3.back, posPlane);
}
// Update is called once per frame
void Update () {
CheckGrab ();
}
void CheckGrab () {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (ray, out hit, distance, HitMaskChar) && Input.GetMouseButtonDown (0)) {
Debug.Log ("is work");
if (!isClone) {
clone = CreatClone (hit.transform.gameObject);
clone.layer = 9;
}
} else if (Physics.Raycast (ray, out hit, distance, HitMaskClone)) {
Grabing (ray);
} else if (isClone) {
DestroyClone ();
}
}
GameObject CreatClone (GameObject Clone) {
isClone = true;
Clone = Instantiate (Clone);
return Clone;
}
void DestroyClone () {
isClone = false;
Destroy (clone);
Debug.Log ("destroy");
}
void Grabing (Ray ray) {
if (plane.Raycast (ray, out distance)) {
clone.transform.position = new Vector3 (ray.GetPoint (distance).x, ray.GetPoint (distance).y - 2f, ray.GetPoint (distance).z);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Get Direction From Raycast 0 Answers
Plane.RayCast not hitting at all 0 Answers
How can I make a third person camera collision script? 2 Answers
Raycast doesn't hit instantiated object within same frame of instantiation. 0 Answers
Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds 0 Answers