查看: 5561|回复: 0
打印 上一主题 下一主题

[经验分享] 代码实现射击游戏中准星和射击的效果

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2013-8-14 23:27:36 |显示全部楼层 |倒序浏览
using UnityEngine;
using System.Collections;
public class BulletBeam : MonoBehaviour  
{

        public  Texture farTexture;
        public  Texture nearTexture;
        private Texture texture;

        public static bool isChange = false;
        void Start ()
        {
                Screen.showCursor = true;
                texture = farTexture;
        }


        void Update ()
        {
                changeTexture();
                myRay();
        }

        void myRay ()
        {
                CharacterController controller = GetComponent<CharacterController>();

                Vector3 position = new Vector3(Screen.width/2 - (texture.width>>1) ,
                                Screen.height/2-(texture.height>>1),10.0f);

                Ray ray = Camera.main.ScreenPointToRay(position);
                Debug.DrawRay (ray.origin, ray.direction * 10, Color.blue);

                //LayerMask layerMaskPlayers = 1 << LayerMask.NameToLayer("Enemy");
                //LayerMask layerMaskTerraim = 1 << //LayerMask.NameToLayer("Terrain");
                //LayerMask FinaMask = (layerMaskTerraim.value | //layerMaskPlayers.value);

                RaycastHit hit;
                if(Input.GetMouseButtonDown(0)||Input.GetMouseButton(0))
                {
                        if(Physics.Raycast(ray,out hit))
                        {
                                if(hit.collider.name == "Cube")
                                {
                                        Instantiate(particle_metal,hit.point,Quaternion.identity);
                                            hit.rigidbody.AddForceAtPosition(ray.direction,hit.transform.position,ForceMode.Impulse);
                                }

                        }

                }

        }

        public void changeTexture()
        {
                if(isChange)
                        texture = nearTexture;
                else
                        texture = farTexture;
        }

        void OnGUI ()
        {
                Rect rect = new Rect(Screen.width/2 - (texture.width>>1) ,
                        Screen.height/2-(texture.height>>1) ,texture.width,texture.height);

                GUI.DrawTexture(rect,texture);
        }
}

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-22 07:25 , Processed in 0.085613 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部