您好,登录后才能下订单哦!
先学习一些基本的脚本实现:
1.动态创建物体.默认位置是(0,0)位置
GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Cube);
  //创建的位置
goNew.transform.position = new Vector3(0, 0, -2);
 goNew.AddComponent<Rigidbody>();//添加刚体组件,是一种泛型
  
  
2.判断用户是否按下鼠标左键
if(Inut.GetMouseButtonDown(0))
  
3.按下鼠标左键,给它一个往前的脉冲力,forward就是一个默认长度为1的单位向量
this.gameObject.rigidbody.AddForce(Vector3.forward * 50, ForceMode.Impulse);
  
  
4.给当前物体添加一个往鼠标点击的方向的多大的力,它就会往那个方向去走
//点击目标然后从摄像机的位置发射出一个小球,这里要计算力的方向向量
 Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3));
  Vector3 dir = targetPos - Camera.main.transform.position;
  //给当前的物体添加某个方向的力
  this.gameObject.rigidbody.AddForce(dir * 5,ForceMode.Impulse);
  
  
5.点击鼠标生成对象
  if (Input.GetMouseButtonDown(0))
  {
       GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Sphere);
       goNew.transform.position = new Vector3(0, 0, 0);
        goNew.AddComponent<Rigidbody>();
  }
  
6.对象销毁回收内存
  if (Input.GetMouseButtonDown(0))
  {
        GameObject s1 = GameObject.Find("Sphere");//相当于document.getElementById();
         Destroy(s1,2); //延时2秒销毁对象
  }
  
  
制作游戏:
using UnityEngine;
  using System.Collections;
  
  
  public class gameText : MonoBehaviour {
  
  
      private GameObject goPlane;
  
  
  // Use this for initialization
  void Start () {
          //找到地形对象
          goPlane = GameObject.Find("Plane");
  
  
          //创建4*4的cube
          for (int i = 0; i < 4; i++)
          {
              for (int j = 0; j < 4; j++)
              {
                  GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                  go.transform.position = new Vector3(i, j, -1);
                  go.AddComponent<Rigidbody>();
                  go.AddComponent<AutoDistory>();//相当于实例化一个脚本销毁对象的一个类然后挂到每个对象中,让他不可见的时候自行销毁
              }
          }
  }
  
  // Update is called once per frame
  void Update () {
          if (Input.GetMouseButtonDown(0))
          {
              //创建×××的object
              GameObject goBullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
              goBullet.transform.position = Camera.main.transform.position;
              goBullet.AddComponent<Rigidbody>();
              //让对象不可见的时候自行销毁
              goBullet.AddComponent<AutoDistory>();
              
              //获取到这个对象的多有资源,在发射的时候播放一个音乐
              goPlane.GetComponent<AudioSource>().Play();
  
  
              //点击鼠标,从摄像机的位置开始发射小球
              Vector3 targetPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 3));
              goBullet.rigidbody.AddForce((targetPos - Camera.main.transform.position) * 20, ForceMode.Impulse);
              
          }
  
  }
      void OnGUI()
          {
              string s = "作者:丁小未";
              GUIStyle bb = new GUIStyle();
              bb.normal.background = null;//设置背景
              bb.normal.textColor = new Color(1,0,0);//设置颜色
              bb.fontSize = 40;
              GUI.Label(new Rect(40, 10, 100, 50), s, bb);
    
          }
  }
  
  
AutoDistory脚本:
using UnityEngine;
  using System.Collections;
  
  //当东西不可见的时候就让他自动销毁
  public class AutoDistory : MonoBehaviour {
  
  // Use this for initialization
  void Start () {
  }
  
  // Update is called once per frame
  void Update () {
  }
  
  
      void OnBecameInvisible()
      {
          Destroy(this.gameObject);
      }
  }
  
  
  
其他提示:
1.天空盒的导入,提醒不要全部导入,不然文件会很大,应用是点击Edit-》Render Setting,然后导入天空盒
2.音频文件是在Camera上添加Component->Audio->Audio Sourse,他自动附带的Audio Listenner
  
详细项目源码:http://download.csdn.net/my
  
  
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:858550 cocos2dx QQ群:280818155
====================== 相互学习,共同进步 ===================
  转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/9734935
  
欢迎关注我的微博:http://weibo.com/u/2590571922
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。