纳金网

标题: 代码实现射击游戏中准星和射击的效果 [打印本页]

作者: 王者再临    时间: 2013-8-14 23:27
标题: 代码实现射击游戏中准星和射击的效果
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);
        }
}


作者: 幽默人生    时间: 2013-12-1 14:02
kankan!!!!!!!!!!!!!!
作者: 幽默人生    时间: 2013-12-3 18:51
Instantiate(particle_metal,hit.point,Quaternion.identity);
这一句有问题。。。
particle_metal没定义
作者: HIDEOKOJIMA    时间: 2014-6-30 14:40
感谢分享!
作者: soulhez    时间: 2014-9-26 16:52
这样写感觉效果不是很好
作者: oelongeo    时间: 2014-9-26 22:41
谢谢王者再临指导 !




欢迎光临 纳金网 (http://www.narkii.com/club/) Powered by Discuz! X2.5