码迷,mamicode.com
首页 > 其他好文 > 详细

unity3d 学习 1

时间:2014-08-26 02:52:55      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   for   ar   2014   

编写目的:unity 学习

编写时间:2014-08-26 02:13

 

发射器

using UnityEngine;
using System.Collections;

//声明 有属性将在检视面板出现
[AddComponentMenu("Camera-Control/Shooter")]

[System.Serializable]
public class Shooter : MonoBehaviour {

    public Transform bullet;

    public float moveSpeed = 0.10f;

    public float power = 1500f;
    
    void Update () {

        /**
         * Input.GetAxis("Horizontal") 返回1 表示水平向右 -1为向左
         * Input.GetAxis("Vertical")   返回1 表示垂直向上 -1为向下
         * */
        float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

        this.transform.Translate(h, v, 0);

        //键盘按下空白键
        if (Input.GetKey(KeyCode.Space))
        {
            //构建一颗子弹,根据检视面板
            Transform instance = Instantiate(bullet, transform.position, transform.rotation) as Transform;

            //取当前前方方向
            Vector3 fwd = transform.TransformDirection(Vector3.forward);

            //给子弹给个发射的力
            instance.rigidbody.AddForce(fwd * power);
        }
    }
}

 

 

子弹 自动销毁

using UnityEngine;
using System.Collections;

public class delBullet : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Destroy(gameObject, 3.0f); //倒数3秒 销毁
    }
}

 

unity3d 学习 1

标签:des   style   blog   color   os   io   for   ar   2014   

原文地址:http://www.cnblogs.com/I-Coding/p/3936304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!