射线方法
Ray ray;
LineRenderer lineRenderer;
// Update is called once per frame
void Awake()
{
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.SetWidth(0.1f, 0.1f);
}
void LateUpdate()
{
ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log("射,我疯狂的射");
lineRenderer.enabled = true;
//Debug.DrawLine(Camera.main.transform.position, hit.point, Color.green);
lineRenderer.SetPosition(1, hit.point);
}
else
{
lineRenderer.enabled=false;
}
}
ViewportPointToRay
Viewport是摄像机的窗口视角(0,0)为左下角 (1,1)为右上角 (0.5,0.5)为中心点
鼠标控制摄像机
Vector3 v3;
float X, Y;
void Update()
{
Y = Input.GetAxis("Mouse X");
X = Input.GetAxis("Mouse Y");
v3 = new Vector3(-X, Y, 0);
Quaternion quaternion = Quaternion.Euler(v3);
transform.rotation = quaternion * transform.rotation ;
}
效果