Survival Shooter Tutorial中文解析(一)

PHASE ONE

  1. 将Environment、Lights置入新建的场景中,设置Position(0,0,0);
  2. 生成GameObject → 3D Object → Quad;重命名为Floor,在Transform中设置Rotation(90,0,0),Scale(100,100,1);
  3. Floor中移除Mesh Renderer组件, layer选择Floor层;
  4. GameObeject →Create Empty 重命名为BackgroundMusic,通过Add Component→ Audio → Audio Source , Audio Clip →Circle Select → Background Music 添加背景音乐,勾选Loop,Volume为0.1。

PHASE TWO

  1. 将Player置入场景中,设置Position(0,0,0),设置Tag为Player;
  2. 新建Animator Controller,命名为PlayerAC,将其置入场景中并双击打开,展开Player模型,将Idle,Move和Death动画拖入PlayerAC中;
  3. 右键点击Idle选择Set as Default,新建一个bool变量命名为IsWalking,新建一个Trigger变量命名为Die;
  4. 右键点击Idle选择Make Transition指向Move,选择生成的流程线设置Condition为IsWalking = true;
  5. 右键点击Move选择Make Transition指向Idle,选择生成的流程线设置Condition为IsWalking = false;
  6. 右键点击Any State选择Make Transition指向Death,选择生成的流程线设置Condition为Die(trigger);
  7. 为Player添加Rigidbody组件,将Drag和Angular Drag设置为Infinity,展开Constraints,锁定Y Position及X、Z Rotations;
  8. 为Player添加组件Capsule Collider,设置Center(0.2,0.6,0),设置Height为1.2;
  9. 为Player添加音乐组件Player Hurt,不勾选Play On Awake;
  10. 为Player编写Scripts,PlayerMovement。
脚本PlayerMovement解析:
using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;

namespace CompleteProject
{
public float speed = 6f;// 定义角色的移动速度
Vector3 movement;// 角色的移动向量
Animator anim; // 角色动画组件
Rigidbody playerRigidbody; //角色刚体组件
int floorMask; // 地板遮罩层用于摄像捕捉
float camRayLength = 100f; //从摄像机到屏幕里的射线长度

void Awake ()
    {
floorMask = LayerMask.GetMask ("Floor");// 创建一个地板的遮罩层
anim = GetComponent  ();//引用Player的组件Animator
playerRigidbody = GetComponent  ();//引用Player的组件Rigidbody
    }
    
void FixedUpdate ()
    {
float h =CrossPlatformInputManager.GetAxisRaw("Horizontal"); // 存储键盘输入的水平坐标
float v = CrossPlatformInputManager.GetAxisRaw("Vertical");// 存储键盘输入的垂直坐标
Move (h, v);//移动Player
Turning ();//使Player跟随鼠标指针转向
Animating (h, v);//Player的动画控制
    }
    
void Move (float h, float v)
     {
movement.Set (h, 0f, v);//根据键盘的输入设置角色的移动向量
movement = movement.normalized * speed * Time.deltaTime;//对键盘的输入向量进行单位化,给它一个速度和帧时间来得到一帧移动的向量;
// Move the player to it's current position plus the movement.
playerRigidbody.MovePosition (transform.position + movement);//用Player的当前坐标加上movement的坐标得到目标位置坐标,移动Player到目标位置
     }

void Turning ()
        {
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);//获取摄像机和鼠标指针所在点连成的线
RaycastHit floorHit;//射线命中点
// Perform the raycast and if it hits something on the floor layer...
        if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))//射线是否命中地板遮罩层
            {
Vector3 playerToMouse = floorHit.point - transform.position;//获取向量从角色位置指向射线命中地板的点
playerToMouse.y = 0f;//确保这个向量始终在地板平面上
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion newRotatation = Quaternion.LookRotation (playerToMouse);//创建一个注视旋转,从垂直上方看着地板上向量playerToMouse的沿着Y轴的旋转轴角
playerRigidbody.MoveRotation (newRotatation);//使Player根据newRotatation这个轴角进行旋转
            }
        }

void Animating (float h, float v)
        {
bool walking = h != 0f || v != 0f;//创建一个布尔值,当键盘输入不为0时,即角色移动时,布尔值为真,否则为假;
anim.SetBool ("IsWalking", walking);//对Player的Animator组件中的条件字段赋值,进行真假判断,来执行角色移动动画或者放置动画;
        }
    }
}

你可能感兴趣的:(Survival Shooter Tutorial中文解析(一))