内容有点乱,刚学没多久,主要记录其中遇到的知识点和问题
Transform
Position: 位置
Rotation:旋转
Scale:大小
Rigidbody : 刚体
刚体学习博客:博客
Collider 碰撞
碰撞学习博客:博客
Is Trigger :允许触发
获取飞机刚体
void Start() { rigidbody = GetComponent
();//获取到了 刚体? } 给飞机某个方向加一个力
float horizontal = Input.GetAxis("Horizontal");//获取横轴 float vertical = Input.GetAxis("Vertical");//获取纵轴 Vector3 movement = new Vector3(horizontal, 0.0f, vertical); //添加一个 速度 = 力方向 * 速度值 rigidbody.velocity = movement * speed;
控制飞机的行动范围 Mathf.Clamp 方法
rigidbody.position = new Vector3( Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax) );
控制飞机倾斜(Quaternion.Euler)
//控制飞机的倾斜度 rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbody.velocity.x * -tilt);
quad和plane的却别
quad和plane是同一类东西,但是quad较小,只由两个三角形组成,plane由200个三角形组成。所以一般quad用于UI贴图等用处,占用资源小。plane当然也可以用于UI,但 占用资源多。如果你想建立一个较大的地形的话还是得用plane。
unity C# Update 与 Fix Update 的区别
参考博客:博客
GameObject类与游戏对象
参考来自:博客
"GameObject类" 1、在脚本中GameObject类是一个类。 2、而在unity中,资源文件夹Assets中的人物模型,也是一个GameObject类。 3、已经unity在"游戏对象"菜单中预设的一些模型,也是GameObject类。 "游戏对象" 与GameObject不同,游戏对象是通过GameObject实例化而来。 (虽然两个都叫game-object,但记住开头大写的一般是类) 凡是通过"Assets"中还是"游戏对象"中创建的类,实例化后都会出现在 "层级" 中, 除了直接在脚本中用GameObject类进行实例化外, "为了方便区分使用下面两种称呼" 【GameObject类】 GameObject类、类 【游戏对象】 游戏对象、对象 "类和对象是什么" 【类】 可以理解为概念、预设、想着中不存在的东西(如同资源文件加中的一大堆模型) 【对象】可以理解为,按照类的概生成一个实际存在的东西(如同出现在场景中的物体) "GameObject与gameObject的区别" 这是一个老生常谈的问题了, 在脚本中"GameObject"是一个类,"gameObject"是一个Unity提前帮我们创建的对象, 这个对象是用来指"拥有该脚本的对象" (ps:脚本也是对象的组件)
根据官方文档查询API,查询getButton :点击鼠标左键进行子弹射击。
点下图的问号就可以查找官方文档了
//官方文档查找GetButton方法,复制 if (Input.GetButton("Fire1") && Time.time > nextTime)//Fire1 鼠标左键 { nextTime = Time.time + fireDelta;//每隔fireDelta射击一次? //第四个方法 GameObject clone = Instantiate(shot, shotSpawn.position, shotSpawn.rotation); // Instantiate:创建一个实例 }
创建网格面的核心就是为其添加2个组件:Mesh Renderer(网格渲染器)和Mesh Filter(网格过滤器
Unity 入门 Random类
下载导入资源包 出现的 问题:
我的解决方法:删掉相关的文件
问题2:
Assets\Scripts\PlayContoller.cs(36,34): error CS0246: The type or namespace name 'Vector' could not be found(are you missing a using directive or an assembly reference?)
解决:Vector打错了,应该是Vector3
问题3:
使用Debug.Log("");报错:
Assets\Scripts\DestoryByContact.cs(40, 13): error CS0104: 'Debug' is an ambiguous reference between 'System.Diagnostics.Debug' and 'UnityEngine.Debug'
我还以为是缺少什么头文件嘞,其实是用法不对,正确用法,可能跟Visual stdio 的版本有关,我是下的20208月最新版
UnityEngine.Debug.Log("");
1、PlayContoller类 飞机相关属性的类
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class Boundary { public float xMax; public float xMin; public float zMax; public float zMin; } public class PlayContoller : MonoBehaviour { private Rigidbody rigidbody;//刚体变量 public float speed;//移动的参数 public Boundary boundary;//边界的参数类 public float tilt;//旋转的参数 public GameObject shot; //子弹类 public Transform shotSpawn;//出现的位置 private float nextTime; public float fireDelta; private AudioSource audioSource; ///子弹音效 // Start is called before the first frame update void Start() { rigidbody = GetComponent
();//获取到了 刚体? audioSource = GetComponent (); } void FixedUpdate() { float horizontal = Input.GetAxis("Horizontal");//获取横轴 float vertical = Input.GetAxis("Vertical");//获取纵轴 Vector3 movement = new Vector3(horizontal, 0.0f, vertical); //添加一个 速度 = 力方向 * 速度值 rigidbody.velocity = movement * speed; //控制范围 rigidbody.position = new Vector3( Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax) ); //控制飞机的倾斜度 rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbody.velocity.x * -tilt); } void Update() { //官方文档查找GetButton方法,复制 if (Input.GetButton("Fire1") && Time.time > nextTime)//Fire1 鼠标左键 { nextTime = Time.time + fireDelta;//每隔fireDelta射击一次? //第四个方法 GameObject clone = Instantiate(shot, shotSpawn.position, shotSpawn.rotation);// Instantiate:创建一个实例 audioSource.Play(); } } } 2、RandomRotator 类,控制行星旋转的类
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RandomRotator : MonoBehaviour { private Rigidbody rigidbody; public float tumble; private void Start() { rigidbody = GetComponent
(); //物体旋转的方法 rigidbody.angularVelocity = Random.insideUnitSphere * tumble; } } 3、DestoryByContact 小行星的类,主要实现 子弹击碎行星,飞机碰行星等
using System.Collections; using System.Collections.Generic; using System.Diagnostics; using UnityEngine; public class DestoryByContact : MonoBehaviour { public GameObject explosion; public GameObject playerExplosion; public int scoreValue; private GameController gameController; private void Start() { GameObject gameControllerObject = GameObject.FindWithTag("GameController"); if(gameControllerObject != null) { gameController = gameControllerObject.GetComponent
(); } else { UnityEngine.Debug.Log("can't find 'GameController' script."); } } private void OnTriggerEnter(Collider other) {//当tag是Boundary时退出触发方法。 if (other.tag == "Boundary") { return; } Instantiate(explosion, other.transform.position, other.transform.rotation);//添加 爆炸效果 if (other.tag == "Player") { Instantiate(playerExplosion, other.transform.position, other.transform.rotation); Destroy(other.gameObject); gameController.GameOver(); } gameController.AddScore(1);//加一分 Destroy(other.gameObject);//销毁子弹 Destroy(gameObject);//销毁行星 } } 4、DestoryByBoundary 类,主要销毁子弹出界、防止冗余很多无用的数据
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestoryByBoundary : MonoBehaviour { private void OnTriggerExit(Collider other) { Destroy(other.gameObject); } }
5、DestoryByTime类,固定时间内 消除对象,这里用于消除行星
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestoryByTime : MonoBehaviour { public float lifetime; private void Start() { Destroy(gameObject, lifetime);//固定时间消失 } }
6、GameController 类,总控制类,控制产生行星、游戏结束、重新开始游戏
using System.Collections; using System.Collections.Generic; using System.Runtime.Hosting; using UnityEngine; using UnityEngine.UI; public class GameController : MonoBehaviour { public GameObject hazard;//冒险 public Vector3 spawnValue;//控制随机数产生的范围 public int hazardCount;// 小行星的数量 public float spawnWait;//d等待时间,一波一波的产生小行星 public float startWait;//开始等待时间??? public float waveWait;//波数等待时间 public Text scoreText; public Text GameOverText; public Text RestartText; private int score;//分数 private bool gameOver; private bool restart; private void Start()//coroutine 协程处理 { gameOver = false; restart = false; GameOverText.text = ""; RestartText.text = ""; score = 0; UpdateScore(); StartCoroutine(SpawnWaves());//??? } private void Update() { if (restart) { if (Input.GetKeyDown(KeyCode.R)) { Application.LoadLevel(Application.loadedLevel); } } } IEnumerator SpawnWaves() {//短时间暂停,让玩家准备下一轮 yield return new WaitForSeconds(startWait); //做一个死循环,满足条件一直做 while (true) { //for点两下tap自动补齐 for (int i = 0; i < hazardCount; i++) { Vector3 spawnPosition = new Vector3(Random.Range(-spawnValue.x, spawnValue.x), spawnValue.y, spawnValue.z); Quaternion spawnRotation = Quaternion.identity; Instantiate(hazard, spawnPosition, spawnRotation); yield return new WaitForSeconds(spawnWait);// } yield return new WaitForSeconds(waveWait); if (gameOver) { RestartText.text = "Press R for Restart"; restart = true; break; } } } public void AddScore(int newScore) { score += newScore; UpdateScore(); } private void UpdateScore() { scoreText.text = "Score:" + score; } public void GameOver() { GameOverText.text = "Game Over!!! Press R for Restart!!"; gameOver = true; } }
7、Mover 类,给一个向前的速度,这里用于 给行星一个固定向下的速度
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Mover : MonoBehaviour { private Rigidbody rigidbody; public float speed; // Start is called before the first frame update void Start() { rigidbody = GetComponent
(); rigidbody.velocity = transform.forward * speed;//向前的速度 } }