美文网首页
子弹发射

子弹发射

作者: pengtuanyuan | 来源:发表于2016-12-05 21:07 被阅读0次
using UnityEngine;
using System.Collections;

public class RayDemo : MonoBehaviour {

    private int x = 10;
    private int y=5;

    public GameObject prefabBrick; 
    public GameObject prefabBullet; 

    private Ray ray;
    private RaycastHit hit;

    private Transform mTransform;

    void Start () {
        mTransform = gameObject.GetComponent<Transform> ();
        CreateWall();
    }
    

    void Update () {
        SendBullet();

    }

    void CreateWall(){
        for(int i=0;i<x;i++){
            for(int j=0;j<y;j++){
                GameObject.Instantiate (prefabBrick,new Vector3(i-5,j,0),Quaternion.identity);
            }
        }   
    }

    void SendBullet(){
        if (Input.GetMouseButtonDown (0)) {
            ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            if (Physics.Raycast (ray, out hit)) {
                GameObject go = GameObject.Instantiate (prefabBullet, mTransform.position, Quaternion.identity) as GameObject;
                Vector3 dir = hit.point - mTransform.position;
                go.GetComponent<Rigidbody> ().AddForce (dir * 100);

            }
        }
    }

}

相关文章

网友评论

      本文标题:子弹发射

      本文链接:https://www.haomeiwen.com/subject/jcjlmttx.html