黑暗奇侠 DarkWonder问题记录(完成)
更多是记录,因为觉得做了,结果只有一个完成的工程有点亏。
34开头是那个系列地34才开始将这个案例,不重置是为了对是视频的索引
34 Terrain(地形)
(问题)Terrain选项卡少了
因为原来功能位置被搬到其他地方
(问题)选项卡不可选
因为开了两个“详情卡”,头一个正常,第二个灰色了
(位置)下载后的位置,导入还挺久的,多了SampleScenes和StandarAssets两个文件夹
C:\Users\wuwei(用户名)\AppData\Roaming\Unity\Asset Store-5.x\Unity Technologies\Unity EssentialsAsset Packs
(资源)自己到商店下载,有树有草,不会自动识别添加,没有选项卡
Standar Assetss/Environment/有四种树木,2种草,6种地表,n种水。80M左右,整个180M左右。
80M左右的那个(提取码:veb6)
(现象)有的包后缀是pkg不识别,改.unitypackage也添加失败
(了解)渲染Baking耗资源
(了解)unity 文件全部折叠
AltLeft
AltRight
如何将unity资源窗体中的文件一下所有折叠/打开
(Terrain数据文件)一块土地对应一份地形数据.asset
地形数据链接了所有资源的路径,不能丢,后缀名是.asset。
它创建是创建地形组件时自动的,暂时找不到手动创建它的方法(复制?)
挺大的300*300的Terrain就有2M左右
(折叠选项卡)Raise or lower terrain
绘制细节中内置的笔刷(除了不能shift,可能是初始高度= =0了)
shift减少地形
(折叠选项卡)Smooth Height(岁月冲刷)
(折叠选项卡)StampTerrain(不能shift)
(折叠选项卡)Paint Texture(植被地表)
Environment中的相关资源,如图所示8种。
默认添加的第一个材质全刷上去
不过这种肥大的视图是认真的吗
缩放一下就好多了
(解决)不知道什么时候出现空白
添加图层时会新建文件,删掉就空白
添加图层时会新建文件,删掉就空白。
替换掉diffuse就可以用新的地表
(演示)
(折叠选项卡)SetHeight
height多高,左键的最高高度
(选项卡)绘制树
(选项卡)绘制细节
太远不显示
受笔刷控制
39 导入资源
Top-Down的那个370M
下载素材
(问题)API Update Required
选了第一项,进去报错,还没有索引解决
(问题)打开demo中的场景,报错
Default reference for field ‘objectFocus’ on script ‘’ contains a reference to itself. This is not allowed, removing reference.
UnityEditorInternal.InternalEditorUtility:ProjectWindowDrag(HierarchyProperty, Boolean)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
Occlusion culling data is out of date. Please rebake
(解决)
叉出去,一个一个拖进来,了解里面有什么。
了解到,Tools scripts里面的脚本都是属于player里面的,其他文件夹都是model
43 绘制地形
有凹陷,事前不先flatten(支棱)一些高度,就不能在凹陷,就是十八层地狱,没有第十九层地狱让你凹陷
46 地形图层
之前看UE4视频的模型介绍,可能有错,以后再说
Diffuse(翻译漫射), 管图案的
法线,管位置怎么贴的
unity terrain不能凹陷
(未了解)地形数据
它是“菜”,terrain图层是“材料”,terrain组件(带相关组件的节点)是“盘子”
(问题)地形不能shift,在已经做好地图下
unity terrain不能凹陷
nity3D 4.2以上版本 Terrain绘制一个下凹(下陷,坑)的地形 详解
stamp中的height == 设置中的地形高度
flatren弄平
现在情况是地图已经制作,方法是做地上河,1,2, 4高度抬上去
48 给环境添加灯光效果
直接挂不能调位置,要加节点
范围
强度
(了解)模型打光
一般是右边(右上,右下,左边,左上,坐=左下)打一个主光;
另一边打一个比主光弱一点的辅光
背后打一个轮廓光
52 (触发器)做一个修练场的修炼计时
碰撞器挂在谁身上,就Tag谁,它老爸也没有用
触发条件(最方便的情况):一触发器,一碰撞器,一刚体给谁都行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cave : MonoBehaviour
{
public float practice_preTime = 3f;//待3秒开始修行
public float practice_duringTime = 9f;//修行需要持续9s
public float timer = 0f;
void OnTriggerStay(Collider other)
{
print("进入修炼法阵");
if (other.tag =="Player")
{
timer+= Time.deltaTime;
if (timer > practice_preTime)
{
print("开始修炼");
timer += Time.deltaTime;
if (timer > practice_duringTime)
{
print("完成修炼");
}
}
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
timer = 0f;
print("终止修炼");
}
}
}
56 给游戏添加技能图片显示
(解决)png不能直接作为GUI的Image的原图像
UI是独立于场景的,就是在屏幕中属于不同的层
57 给游戏添加修炼倒计时文字显示
(了解) _instance 静态类
定义
public static UI_ES _instance;
void Start()
{
_instance = this;
}
调用
UI_ES._instance.message.GetComponent<Text>().text = "正在修炼中......";
(了解)数字取整,小数位
Mathf.FloorToInt()
.ToString(“0.00”)
float resPreTime = Mathf.FloorToInt(practice_preTime - timer);
或者
string resPreTime = (practice_preTime - timer).ToString("0.00");
UI_ES._instance.message.GetComponent<Text>().text += "\n准备修炼剩余时间:" + resPreTime + "秒";
58 场景设计–认识水资源和添加水平面
这个资源的水
62 62 在环境中添加巨魔
(了解)position与localPosition
position,他是国家的主席
localPosition,他也是一个儿子的父亲
(移动)transform.forward
(旋转)transform.Rotate(trasform.forward * 90)
63 实现巨魔的ai控制行走(智能移动)
(了解)随机数
包括左边min,不包括右边max
print(Random.Range(0,10));
print(Random.Range(100f, 200f));
(问题)闪现旋转的问题
因为旋转的最终值和旋转是一步到位的
解决,旋转的最终值和旋转分开来。在重置(只会执行一次,方便后面不断被削减)中赋最终值,在
原来的"一步到位"
if (timer > idleTime && hasRotated==false)//旋转
{
hasRotated = true;
//transform.Rotate(transform.up * Random.Range(-90, 90));
transform.Rotate( new Vector3(0, Random.Range(-90, 90) ,0));
}
现在的分开来
targetY的赋值在重置的方法体里面(相对只会执行一次)
void AIMove()
{
timer += Time.deltaTime;
if (Mathf.Abs(targetY) > 0.2f )//太小不转,参考“省去最后一刀”,考古文物的清理最后
{
float rotateRate = 0.05f;//2f * Time.deltaTime;
float deltaY = targetY * rotateRate ;//
transform.Rotate(new Vector3(0, deltaY, 0));
targetY -= deltaY;
}
......
if (timer > idleTime + rotateTime + runTime)//重置
{
targetY = Random.Range(-90f, 90f);
isIdle = true;
timer = 0f;
}
(了解)Time.deltaTime
Time.deltaTime=0.016 == /60 (大约,对初学者直接认为等于,没有区分多大约的必要)(大约在冬季)
Time.fixedDeltaTime,FixedUpdate中出现的(就是在冬季,还是大寒)
上面的 0.05f 和 2f * Time.deltaTime 效果差不多
2*0.016 = 0.032(算上0.016的大约(刚说完初学者不用区分,现在就打脸)和眼睛的误差, 差不多)
63 (角色控制器,“捕捉穿山甲”)实现巨魔的ai控制行走(智能移动)
private CharacterController characterController;
void Start()
{
characterController = GetComponent<CharacterController>();
......
if (timer > idleTime + rotateTime)//walk
{
isIdle = false;
//transform.position += transform.forward * Time.deltaTime * speed;
characterController.Move(transform.forward * Time.deltaTime *speed);
}
不“穿山甲”了
65 (计时器和预制体实例)添加巨魔孵化器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrollSpawn : MonoBehaviour
{
//生成时间
public float timer = 0f;
public float spawnTime = 3f;
public GameObject trollPrefab;
//生成数量
public int count = 0;
public int maxCount = 2;
void Update()
{
timer += Time.deltaTime;
if (timer > spawnTime && count<maxCount)
{
timer = 0f;
Instantiate(trollPrefab, transform.position, Quaternion.identity);
count ++;
}
}
}
66 按键监听(键盘,鼠标,自定义键)
(了解)Fire1设置有两个
(了解)备选 Alt
说被备不备选的,其实优先级还不确定
轴是渐变的,-1 0 1
67 (应改进)给人物添加技能释放功能
比如技能释放的位置,但还没实践过
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed=10f;
//技能
public bool getSkill = false;
public float timer = 0f;
public float coolTime = 5f;
public bool isCooled = false;//未习得技能,未冷却
public GameObject skillPrefab;
// Update is called once per frame
void Update()
{
//移动
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 offset = transform.position;
transform.position += new Vector3(x * Time.deltaTime * speed, 0 , z * Time.deltaTime * speed);
//冷却
if (timer < coolTime && isCooled == false)
{
timer += Time.deltaTime;
}
if (timer > coolTime && isCooled==false)
{
timer = 0;
isCooled = true;
}
//释放技能
if ( Input.GetButtonDown("Fire1"))
{
if (!getSkill)
{
print("未习得魔法");
}
else if (!isCooled)
{
print("技能冷却中");
}
else if(getSkill && isCooled)
{
print("大威天龙,世尊地藏,般若诸佛,般若巴嘛空!");
Vector3 targetPos = GameObject.FindGameObjectWithTag("Player").transform.position;
Instantiate(skillPrefab, targetPos,Quaternion.identity);
isCooled = false;
}
}
}
}
68 添加技能魔法跟巨魔之间的攻击
给巨怪加上Enemy标签,和碰撞体
技能的那个特效,加触发器
(问题)巨怪死了,又idle起来
动画器中 death -> idle,是无条件的连通,删掉(问题是,)
计时,几秒后,销毁节点
(问题)死了尸体会移动(加isDead参数)
(问题)血量都没了,还在受伤动画
(100血,一次50伤害, get_hit动画多(8)次,初步估计;set1之后,要set0回来,不然一直执行get_hurt动画;试试加 退出时间)
private void OnTriggerExit(Collider other)
{
if (other.tag == "Enemy")
{
Troll troll = other.GetComponent<Troll>();
troll.GetComponent<Animator>().SetFloat("get_hit", 0f);
}
}
(解问题)缺少死亡动画
动画器参数写错了
(没解决)有时会走“空气地”,在树上走
(没加碰撞器,身上只有角色控制器械 和 触发器; 巨怪是胶囊胶囊撞器,底部是圆的,容易被玩家的碰撞器垫高)
(问题)死了尸体不动,但是不销毁
Destroy(this.gameObject);
(不知道什么时候不会了)一开始在触发器内,触发的不灵敏,好像地走到边界
(问题)health==0后,isDead还是false,一直在计时
(添加OntriggerExit, get_hit设回0; health减去damage后要判断小于等于0死亡,而不是减去damage与小于等于0成两个支路)
public void Hurted(float damage)//受伤或死亡
{
if (isDead==false && health > 0)
{
health = (health - damage) > 0 ? (health - damage) : 0f;
animator.SetFloat("get_hit", 1f);
}
if (health <= 0)
{
Dead();
}
}
(不知道什么时候不会了)减血的时间和动画非常不即时
69 (3种方式销毁节点)控制魔法消失和巨魔的消失
都不好意思说3种,1、2是重载
Destroy(this.gameObject);
void DestroySkill_01()
{
skillLifeTime -= Time.deltaTime;
if (skillLifeTime < 0)
{
Destroy(this.gameObject);
}
}
Destroy(this.gameObject, time);
void DestroySkill_02()
{
Destroy(this.gameObject, skillLifeTime);
}
开协程
StartCoroutine((IEnumerator)IEDestroyByTime( skillLifeTime ) );//出生就注定7秒后死掉
//携程销毁节点
IEnumerable IEDestroyByTime(float time)
{
yield return new WaitForSeconds(time);//开携程等n秒
Destroy(this.gameObject);
}
70 引入恐龙资源,查看恐龙的播放动画
(了解)霸王龙 雷克斯暴龙 (Trex)Tyrannosaurus Rex
public void OnGUI()
{
if (GUI.Button(new Rect((float)50, (float)50, (float)80, (float)20), "Idle"))//x,y,宽,高
{
this.animation.CrossFade("idle");
}
......
(播放动画)this.animation.CrossFade(“idle”);
71 在场景中添加恐龙
(问题)恐龙模型以x轴正方向为forward,unity以z为前
(技巧)先进去重置再出来,得到该处的世界坐标
背景是要有一个父节点,来管理恐龙和相机
71 在场景中添加恐龙
(了解)角色控制器
有碰撞效果
Unity角色控制器CharacterController的简单介绍
(了解未深刻)Move 与 SimpleMove
public CollisionFlags Move(Vector3 motion);
A more complex move function taking absolute movement deltas.
一个更复杂的移动函数,采用绝对移动增量。
public bool SimpleMove(Vector3 speed);
Moves the character with speed.
快速移动角色。
72 72 添加恐龙的控制
(问题)旋转没问题,移动横着走
(我以为)父节点摆正方向就会很好
结果运行了,恐龙坐标轴,我为了摆正方向做的父节点方向不正确了
试到最后可用的代码
可不是父节点左方向是让恐龙向前
void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
transform.Rotate(new Vector3(0, x * 30 * Time.deltaTime, 0));
characterController.Move(-transform.right * y * speed *Time.deltaTime);
(解决)不要旋转父节点
有的模型的正方向,运行前不正常,在运行后正常
(恐龙移动代码)
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
characterController.Move(transform.forward * y * speed *Time.deltaTime);//移动
transform.Rotate(new Vector3(0, x * 30 * Time.deltaTime, 0));//旋转
if (Mathf.Abs(y) > 0.1f)//移动太小不动画
{
trexAnimation.CrossFade("walk_loop");
}
else
{
trexAnimation.CrossFade("idle");
}
78 设计触发区域,设计友情提示
注意Trigger条件,trigger collider rigidBody
UI_ES(UI_EventSystem的意思)
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
UI_ES._instance.message.gameObject.SetActive(true);
UI_ES._instance.message.GetComponent<Text>().text = "你太弱了,去找暴龙哥帮忙!";
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
UI_ES._instance.message.gameObject.SetActive(false);
}
}
79 添加恐龙的攻击
(问题)恐龙的撕咬动画几乎没有,太快或者没执行
不用GetButtonDown,用GetButton
Unity GetButtonDown、GetButton、GetButtonUp的区别
所以只有GetButton的有足够的时间让动画播放完整
(问题)围栏没有识别到恐龙的攻击
碰撞器太小,小于等于角色控制器的
Trex.cs
void Attack()
{
if (Input.GetButton("Fire1"))
{
print("attack");
isAttack = true;
animation.CrossFade("Snap");
}
if (Input.GetButtonUp("Fire1"))
{
isAttack = false;
}
}
Barrier.cs
private void OnTriggerStay(Collider other)//显示对话
{
if (other.tag == "Trex")//被恐龙摧毁围栏,恐龙处于攻击状态
{
print("围栏被破坏");
if (other.GetComponent<Trex>().isAttack == true)
{
Destroy(barrierGo.gameObject);//销毁围栏
Destroy(this.gameObject);//销毁围栏对应的碰撞体,我分开放
}
}
}
81 设计跟恐龙的对话框
(问题)Player与恐龙碰撞时有时候会掉地下
运行后就慢慢往下掉,穿地那种,因为加了刚体没有打钩 运动学的
(未解决)恐龙悬空
被玩家给顶起来
(问题)碰撞器和触发器能挂在同一个节点下
unity同一个gameobject上可以同时存在碰撞体和触发器吗
83 设计恐龙和主角的控制切换
(了解)找组件的方法汇总
【Unity3D游戏开发】GameObject.Find()、Transform.Find查找隐藏对象 (十)
(了解)找一级子节点
(问题)控制子节点的active
我在围栏的触发器,想要玩家的子节点(带相机的节点)生效,就是从恐龙手上夺回主相机
第一种没成功,第二种后才成功了
//playerGo是拖节点赋值
//静态类Player._instance也可以
//playerGo是拖节点赋值的
//playerGo.GetComponentInChildren<Camera>().gameObject.SetActive(true);//01失败
//playerGo.transform.Find("Camera").gameObject.SetActive(true);//02成功
//或者
//Player._instance.GetComponentInChildren<Camera>().gameObject.SetActive(true);//03失败
Player._instance.transform.Find("Camera").gameObject.SetActive(true);//04成功
85 设计游戏的剧情ui
选中所有图片直接拖上去顺序乱的,只能一个一个来
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Dialogue : MonoBehaviour
{
public Sprite[] sprites;
private Sprite sprite;
private int index = 0;
// Start is called before the first frame update
void Start()
{
GetComponentInChildren<Image>().sprite=sprites[0];
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
index++;
if (index < 10)
{
GetComponentInChildren<Image>().sprite = sprites[index];
}
else
{
//Application.LoadLevel(1);//过时
SceneManager.LoadScene(1);
}
}
}
}