首页 > 编程语言 > 详细

unity, 鼠标与场景交点

时间:2015-05-24 01:17:11      阅读:274      评论:0      收藏:0      [点我收藏+]

在鼠标与场景交点上放一个mark,并于1s后消失:

新建一个空GameObject,命名为moushHitTest,添加下面脚本:

using UnityEngine;
using System.Collections;

public class mouseHit : MonoBehaviour {
    public GameObject m_moushHitMarkPrefab;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    

        if (Input.GetMouseButtonDown (0)) {//left button down
            Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hitInfo;
            float camRayLength = 100f;
            bool isHit = Physics.Raycast (camRay,out hitInfo,camRayLength);
            if (isHit) {
                Debug.Log(hitInfo.point);
                Object instance=Instantiate (m_moushHitMarkPrefab, hitInfo.point, Quaternion.identity);
                instance.name="hitMark";
                //about startCoroutine and yield: http://stackoverflow.com/questions/12932306/how-does-startcoroutine-yield-return-pattern-really-work-in-unity
                StartCoroutine(delayDestroy(instance));

            }
        }

    }

    IEnumerator delayDestroy(Object instance){
        yield return new WaitForSeconds (1.0f);
        Destroy (instance);
    }

}

unity, 鼠标与场景交点

原文:http://www.cnblogs.com/wantnon/p/4525247.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!