public void OnCollisionEnter(Collision collision) { CC_Status_Change.EndFollow(collision.gameObject); //碰撞后物体即不被选中,这时应使箭头消失 CC_RayController.instance.item = null; //Destroy(collision.gameObject.GetComponent<Rigidbody>()); //触发后销毁刚体组件,防止多次触发 //collision.gameObject.layer=2; string COLLISION = collision.gameObject.name.ToUpper(); GameObject GrandPa = gameObject.transform.parent.parent.gameObject; string gradpa = GrandPa.transform.name.Replace("(Clone)", ""); if (COLLISION == gradpa) { Destroy(collision.gameObject.GetComponent <Rigidbody>()); //触发后销毁刚体组件,防止多次触发 AudioSource.PlayClipAtPoint(T, transform.position); //播放正确音效 Destroy(GrandPa, 1.5f); //1.5s后本题消失,2s后下一题产生 //因为Destory之后未赋值为null,所以设置一个标记变量 isDestroyed = true; if (CC_PositiveAnswerRate._instance.PositiveNum + CC_PositiveAnswerRate._instance.NegativeNum < CC_PositiveAnswerRate._instance.CurrentNO) { CC_PositiveAnswerRate._instance.PositiveNum++; //正确数目加一 } } else if (COLLISION == null) { //未发生碰撞 } else { AudioSource.PlayClipAtPoint(F, transform.localPosition); Destroy(collision.gameObject); //播放错误声音 if (CC_PositiveAnswerRate._instance.PositiveNum + CC_PositiveAnswerRate._instance.NegativeNum < CC_PositiveAnswerRate._instance.CurrentNO) { CC_PositiveAnswerRate._instance.NegativeNum++; //因为咱们的题目可以容许多次答错,故需要做一个判断 } } }
void DrawLine() { #if UNITY_EDITOR #else if (Input.touchCount == 0) { return; } #endif Ray ray; if (Input.touchCount > 0) { ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); RaycastHit hitInfo; //存储射线碰撞到的第一个对象信息 if (Physics.Raycast(ray, out hitInfo)) //点击的是物体 { if (item == null) //之前未选中物体 { //第一次选中 item = hitInfo.transform.gameObject; CC_Status_Change.StartFollow(item); } else if (item != hitInfo.transform.gameObject) //之前选中了物体,但不是当前触碰的物体 { //切换物体 CC_Status_Change.EndFollow(item); item = hitInfo.transform.gameObject; CC_Status_Change.StartFollow(item); } } else //点击的是空白处 { if (item != null) { //选中空白 CC_Status_Change.EndFollow(item); item = null; } } } }