using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mouse : MonoBehaviour
{
public GameObject ballHalf;
public GameObject cubeHalf;
public GameObject coneTopHalf;
public GameObject coneHalf;
GameObject Obj = null;
// Start is called before the first frame update
void Start()
{
}
Ray GenerateMouseRay(Vector3 touchPos)
{
Vector3 mousePosFar = new Vector3(touchPos.x, touchPos.y, Camera.main.farClipPlane);
Vector3 mousePosNear = new Vector3(touchPos.x, touchPos.y, Camera.main.farClipPlane);
Vector3 mousePosF = Camera.main.ScreenToViewportPoint(mousePosFar);
Vector3 mousePosN = Camera.main.ScreenToViewportPoint(mousePosNear);
Ray mr = new Ray(mousePosN, mousePosF - mousePosN);
return mr;
}
// Update is called once per frame
void Update()
{
if (((Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Moved) || Input.GetMouseButton(0)))
{
Plane objPlane = new Plane(Camera.main.transform.forward * -1, this.transform.position);
Ray mRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float rayDistance;
if(objPlane.Raycast(mRay,out rayDistance))
{
this.transform.position = mRay.GetPoint(rayDistance);
}
Ray mouseRay = GenerateMouseRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(mouseRay.origin,mouseRay.direction,out hit))
{
bool a = Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit);
Obj = hit.transform.gameObject;
Debug.Log(hit.transform.tag);
if (Obj.tag == "ball")
{
GameObject a1 = (GameObject)Instantiate(ballHalf, Obj.transform.localPosition, Obj.transform.localRotation);
a1.transform.rotation *= Quaternion.Euler(0,0,90);
a1.transform.Translate(3f, 0, 0);
a1.GetComponent<Rigidbody>().velocity = Obj.GetComponent<Rigidbody>().velocity;
GameObject a2 = (GameObject)Instantiate(ballHalf, Obj.transform.localPosition, Obj.transform.localRotation);
a2.transform.rotation *= Quaternion.Euler(0, 0, -90);
a2.GetComponent<Rigidbody>().velocity = Obj.GetComponent<Rigidbody>().velocity;
Destroy(Obj);
}
}
}
}
}
里面的刚体,碰撞器都有安装