using UnityEngine;
using System.Collections;
public class RayPractice : MonoBehaviour {
private Camera mainCamera;
private Renderer render;
[SerializeField]
private Material redMat;
[SerializeField]
private Material greenMat;
// Use this for initialization
void Start () {
if(mainCamera == null)
mainCamera = Camera.main;
if (render == null)
render = GetComponent<Renderer> ();
}
// Update is called once per frame
void Update () {
if (mainCamera == null || render == null) {
return;
}
if (Input.GetMouseButtonDown (0)) {
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit)){
string objName = hit.collider.name;
if(objName.Equals("Cube")){
render.material = redMat;
}
else{
render.material = greenMat;
}
}
}
}
}
用射线实现3D点击效果
最新推荐文章于 2022-06-26 16:28:25 发布