黑暗之光 Day1

1. 设置鼠标指针图标

  Build Setting -> Player Setting

  

2. 添加雾的效果 

   Window->Lighting->Settings。

  

3. 任意键按下事件

 1 // Use this for initialization
 2 void Start () {
 3     // 查找 ButtonContainer 实体
 4     buttonContainer = this.transform.parent.Find("ButtonContainer").gameObject;
 5 }
 6     
 7 // Update is called once per frame
 8 void Update () {
 9     if (!isAnyKeyDown)
10     {
11         if (Input.anyKey)       // 任意键按下
12         {
13             ShowButton();       // 显示按钮界面
14         }
15     }
16 }
17 
18 void ShowButton()
19 {
20     buttonContainer.SetActive(true);    // 显示按钮界面
21     this.gameObject.SetActive(false);   // 隐藏 pressanykey 
22     isAnyKeyDown = true;
23 }

 4. 添加游戏背景音乐和鼠标点击声音

   Audio Source

    

  NGUI Play Sound

    

5. 点击 OK 按钮保存数据

1 public void OnOkButtonClick()
2 {
3     PlayerPrefs.SetInt("SelectedCharacterIndex", selectIndex);      // 保存数据
4     PlayerPrefs.SetString("Name", nameInput.value);
5     // TODO 转到下一场景
6 }

 

 6. 根据鼠标点击改变人物朝向

1 // 射线检测
2 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
3 RaycastHit hitInfo;
4 bool isCollider = Physics.Raycast(ray, out hitInfo);
5 if (isCollider && hitInfo.collider.tag == Tags.ground)
6 {
7     LookAtTarget(hitInfo.point);        // 朝向目标位置
8 }
1 // 朝向目标位置
2 void LookAtTarget(Vector3 target)
3 {
4     target.y = transform.position.y;        // 绕y轴旋转,注意高度要保持一致
5     transform.LookAt(target);               // 朝向目标
6 }

7. 控制人物移动

   Character Controller。

    

1 void Update () {
2     // 得到当前位置与目标位置的距离
3     float distance = Vector3.Distance(dir.targetPos, transform.position);
4     if (distance > 0.1f)        // 若没有到达目标位置
5     {
6         // 简单移动
7         controller.SimpleMove(transform.forward * speed);
8     }
9 }

 8. 人物移动动画的播放

  

 1 // LateUpdate 运行稍晚于 Update
 2 void LateUpdate () {
 3     if (move.state == CharacterState.Moving)    // 移动状态
 4     {
 5         PlayAnimation("Run");
 6     }
 7     else if(move.state == CharacterState.Idle)  // 静止状态
 8     {
 9         PlayAnimation("Idle");
10     }
11 }
12 
13 // 播放animationName动画
14 void PlayAnimation(string animationName)
15 {
16     animation.CrossFade(animationName, 0.1f);
17 }

 9. 相机随人物移动

 1 // Use this for initialization
 2 void Start () {
 3     // 获取人物位置
 4     player = GameObject.FindGameObjectWithTag(Tags.player).transform;
 5     offset = transform.position - player.position;          // 人物位置与相机偏移
 6     transform.LookAt(player);                               // 相机看向人物
 7 }
 8     
 9 // Update is called once per frame
10 void Update () {
11     transform.position = player.position + offset;          // 相机跟随人物
12 }

10. 控制视野的远近

1 // 鼠标中轴控制视野的远近
2 void ScrollView()
3 {
4     distance = offset.magnitude;                     // 相机与人物的距离
5     // 根据中轴控制视野远近
6     distance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
7     offset = offset.normalized * distance;
8 }

11. 控制视野的旋转

 1 // 控制视野旋转
 2 void RotateView()
 3 {
 4     if (Input.GetMouseButtonDown(0))        // 监听鼠标左键
 5     {
 6         isRotate = true;
 7     }
 8     if (Input.GetMouseButtonUp(0))
 9     {
10         isRotate = false;
11     }
12     if (isRotate)
13     {
14         // 以人物为中心,绕y轴旋转
15         transform.RotateAround(player.position, Vector3.up, Input.GetAxis("Mouse X")*rotateSpeed);
16         Vector3 originalPos = transform.position;           // 记录当前位置和旋转
17         Quaternion originalRot = transform.rotation;
18         // 以人物为中心,绕视野x轴旋转
19         transform.RotateAround(player.position, transform.right, -Input.GetAxis("Mouse Y")*rotateSpeed);
20         float x = transform.eulerAngles.x;
21         if (x < 10 || x > 80)                               // 控制上下旋转范围
22         {
23             transform.position = originalPos;
24             transform.rotation = originalRot;
25         }
26 
27         offset = transform.position - player.position;
28     }
29 }
原文地址:https://www.cnblogs.com/coderJiebao/p/unity3d04.html