Unity 黑暗之光 笔记 第三章

第三章 角色控制

 

1.创建游戏运行场景并导入素材资源

2.创建和管理标签

1        //const 表明这个是一个共有的不可变的变量
2        public const string ground = "Ground";//地面标签
3        public const string player = "Player";//人物标签

3.实现角色行走点击效果

给角色添加PlayerDir点击效果代码

 1        public GameObject effect_click_prefab;
 2        void Update ()
 3        {
 4         if (Input.GetMouseButtonDown(0))
 5         {
 6                      Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);//发射射线
 7                      RaycastHit hitInfo;
 8                      //使用射线进行检测
 9                      bool isCollder = Physics.Raycast(ray, out hitInfo);
10             if (isCollder && hitInfo.collider.tag == Tags.ground)
11             {
12                            //实例化点击的效果
13                            ShowClickEffect(hitInfo.point);
14             }
15         }
16        }
17        void ShowClickEffect(Vector3 hitPoint)
18        {
19               hitPoint = new Vector3(hitPoint.x, hitPoint.y + 0.188888888f,  hitPoint.z);
20               GameObject.Instantiate(effect_click_prefab, hitPoint,  Quaternion.identity);
21        }

4.控制角色移动朝向

添加控制主角移动并优化PlayerDir代码

       public GameObject effect_click_prefab;
       private bool isMoving = false;//表示鼠标是否按下
       bublic Vector3 targetPosition = Vector3.zero;
       void Update ()
       {
        if (Input.GetMouseButtonDown(0))
        {
                     Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);//发射射线
                     RaycastHit hitInfo;
                     //使用射线进行检测
                     bool isCollder = Physics.Raycast(ray, out hitInfo);
            if (isCollder && hitInfo.collider.tag == Tags.ground)
            {
                isMoving = true;
                           //实例化点击的效果
                           ShowClickEffect(hitInfo.point);
                LookAtTarger(hitInfo.point);
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
                     isMoving = false;
        }
        //点击按下之后一直朝向鼠标位置
        if (isMoving)
        {
            //得到要移动的目标位置
            //让主角朝向目标位置
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//发射射线
            RaycastHit hitInfo;
            //使用射线进行检测
            bool isCollder = Physics.Raycast(ray, out hitInfo);
            if (isCollder && hitInfo.collider.tag == Tags.ground)
            {
                LookAtTarger(hitInfo.point);
            }
        }
       }
    /// <summary>
    /// 实例化点击效果
    /// </summary>
    /// <param name="hitPoint"></param>
       void ShowClickEffect(Vector3 hitPoint)
       {
              hitPoint = new Vector3(hitPoint.x, hitPoint.y + 0.188888888f,  hitPoint.z);
              GameObject.Instantiate(effect_click_prefab, hitPoint,  Quaternion.identity);
       }
    /// <summary>
    /// 主角朝向位置
    /// </summary>
    /// <param name="hitPoint"></param>
    void LookAtTarger(Vector3 hitPoint)
    {
        targetPosition = hitPoint;
        targetPosition = new Vector3(targetPosition.x, transform.position.y,  targetPosition.z);
        this.transform.LookAt(targetPosition);
    }

5.控制角色的移动

给角色添加Character Controller碰撞体组件

添加PlayerMove代码控制移动

 1     public float speed = 1f;//移动速度
 2     private PlayerDir dir;
 3     private CharacterController controller;
 4     void Start()
 5     {
 6         dir = this.GetComponent<PlayerDir>();
 7         controller = this.GetComponent<CharacterController>();
 8     }
 9     void Update()
10     {
11         float distance = Vector3.Distance(dir.targetPosition,transform.position);
12         if (distance > 0.5f)
13         {
14             controller.SimpleMove(transform.forward * speed);
15         }
16     }

6.控制角色移动动画播放

添加角色的移动动画

 

为PlayerMove代码添加枚举并更改Update

 1 public enum PlayerState
 2 {
 3     Moving,
 4     Idle
 5 }
 6     public PlayerState state = PlayerState.Idle;
 7  
 8     void Update()
 9     {
10         float distance = Vector3.Distance(dir.targetPosition,transform.position);
11         if (distance > 0.5f)
12         {
13             state = PlayerState.Moving;
14             controller.SimpleMove(transform.forward * speed);
15         }
16         else
17         {
18             state = PlayerState.Idle;
19         }

 

新建PlayerAnimation动画切换代码

 1        private PlayerMove move;
 2        void Start ()
 3        {
 4               move = this.GetComponent<PlayerMove>();
 5        }
 6        
 7        void LateUpdate ()
 8        {
 9         if (move.state == PlayerState.Moving)
10         {
11                      PlayAnim("Walk");
12         }
13         else if (move.state == PlayerState.Idle)
14         {
15             PlayAnim("Idle");
16         }
17        }
18        void PlayAnim(string animName)
19        {
20               //animation.CrossFade(animName);
21               GetComponent<Animation>().CrossFade(animName);
22        }

 

7.添加控制相机跟随主角移动

给主相机添加FollowPlayer脚本并给人物添加Player标签

 1        private Transform player;
 2        private Vector3 offsetPosltion;//位置偏移
 3        void Start ()
 4        {
 5               player = GameObject.FindGameObjectWithTag(Tags.player).transform;
 6               transform.LookAt(player.position);
 7               offsetPosltion = transform.position - player.position;
 8        }
 9        
10        void Update ()
11        {
12               transform.position = offsetPosltion + player.position;
13        }

 

8.控制视野的拉近和拉远

FollowPlayer代码中定义变量distancescrollSpeed

 1        public float distance = 0f;//人物与相机的距离
 2        public float scrollSpeed = 5f;//相机移动速度
 3 添加scrollView方法并在Update中引用
 4        /// <summary>
 5        /// 视野的拉近和拉远效果 
 6        /// </summary>
 7        void ScrollView()
 8        {
 9               print(Input.GetAxis("Mouse ScrollWheel"));//向前滑动返回正值 向后滑动返回负值
10               distance = offsetPosltion.magnitude;//位置偏移的magnitude值
11               distance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
12               distance = Mathf.Clamp(distance, 2, 18);//距离限制
13               offsetPosltion = offsetPosltion.normalized * distance;
14        }

 

9.控制视野的旋转

FollowPlayer代码中定义变量isRotatingrotateSpeed

1        private bool isRotating = false;
2        public float rotateSpeed = 1f;//相机旋转速度

添加RotateView方法并在Update中引用

 1        /// <summary>
 2        /// 视野的旋转
 3        /// </summary>
 4        void RotateView()
 5        {
 6         //Input.GetAxis("Mouse X");//得到鼠标在水平方向的滑动
 7         //Input.GetAxis("Mouse Y");//得到鼠标在垂直方向的滑动
 8         if (Input.GetMouseButtonDown(1))
 9         {
10                      isRotating = true;
11         }
12         if (Input.GetMouseButtonUp(1))
13         {
14                      isRotating = false;
15         }
16         if (isRotating)
17         {
18                      transform.RotateAround(player.position, player.up,  rotateSpeed * Input.GetAxis("Mouse X"));
19             Vector3 originalPos = transform.position;
20             Quaternion originalRotation = transform.rotation;
21             transform.RotateAround(player.position, transform.right, -rotateSpeed *  Input.GetAxis("Mouse Y"));//影响属性有两个 position rotation
22             float x = transform.eulerAngles.x;
23             if (x < 10 || x > 80)
24             {//当超出范围之后,将属性归位原来的,让旋转无效
25                 transform.position = originalPos;
26                 transform.rotation = originalRotation;
27             }
28         }
29         offsetPosltion = transform.position - player.position;
30     }

 

10.角色添加状态信息

给角色添加PlayerStatus代码

 1     public int grade = 1;//等级
 2     public int hp = 100;//血量
 3     public int mp = 100;//蓝量
 4     public int coin = 200;//金币数量
 5     public int attack = 20;
 6     public int attack_plus = 0;
 7     public int def = 20;
 8     public int def_plus = 0;
 9     public int speed = 20;
10     public int speed_plus = 0;
11     public int point_remain = 0;//剩余的点数

 

11.修复BUG

修改PlayerDir

 1        public GameObject effect_click_prefab;
 2     public Vector3 targetPosition = Vector3.zero;
 3     private bool isMoving = false;//表示鼠标是否按下
 4     private PlayerMove playerMove;
 5     void Start()
 6     {
 7         targetPosition = transform.position;
 8         playerMove = this.GetComponent<PlayerMove>();
 9     }
10        void Update ()
11        {
12         if (Input.GetMouseButtonDown(0))
13         {
14                      Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);//发射射线
15                      RaycastHit hitInfo;
16                      //使用射线进行检测
17                      bool isCollder = Physics.Raycast(ray, out hitInfo);
18             if (isCollder && hitInfo.collider.tag == Tags.ground)
19             {
20                 isMoving = true;
21                            //实例化点击的效果
22                            ShowClickEffect(hitInfo.point);
23                 LookAtTarger(hitInfo.point);
24             }
25         }
26         if (Input.GetMouseButtonUp(0))
27         {
28                      isMoving = false;
29         }
30         //点击按下之后一直朝向鼠标位置
31         if (isMoving)
32         {
33             //得到要移动的目标位置
34             //让主角朝向目标位置
35             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//发射射线
36             RaycastHit hitInfo;
37             //使用射线进行检测
38             bool isCollder = Physics.Raycast(ray, out hitInfo);
39             if (isCollder && hitInfo.collider.tag == Tags.ground)
40             {
41                 LookAtTarger(hitInfo.point);
42             }
43         }
44         else
45         {
46             if (playerMove.isMoving)
47             {
48                 LookAtTarger(targetPosition);
49             }
50         }
51        }
52     /// <summary>
53     /// 实例化点击效果
54     /// </summary>
55     /// <param name="hitPoint"></param>
56        void ShowClickEffect(Vector3 hitPoint)
57        {
58               hitPoint = new Vector3(hitPoint.x, hitPoint.y + 0.188888888f,  hitPoint.z);
59               GameObject.Instantiate(effect_click_prefab, hitPoint,  Quaternion.identity);
60        }
61     /// <summary>
62     /// 主角朝向位置
63     /// </summary>
64     /// <param name="hitPoint"></param>
65     void LookAtTarger(Vector3 hitPoint)
66     {
67         targetPosition = hitPoint;
68         targetPosition = new Vector3(targetPosition.x, transform.position.y,  targetPosition.z);
69         this.transform.LookAt(targetPosition);
70     }

修改PlayerMove方法

 1     public float speed = 1f;//移动速度
 2     public PlayerState state = PlayerState.Idle;
 3     public bool isMoving = false;
 4     private PlayerDir dir;
 5     private CharacterController controller;
 6     void Start()
 7     {
 8         dir = this.GetComponent<PlayerDir>();
 9         controller = this.GetComponent<CharacterController>();
10     }
11     void Update()
12     {
13         float distance = Vector3.Distance(dir.targetPosition,transform.position);
14         if (distance > 0.5f)
15         {
16             isMoving = true;
17             state = PlayerState.Moving;
18             controller.SimpleMove(transform.forward * speed);
19         }
20         else
21         {
22             isMoving = false;
23             state = PlayerState.Idle;
24         }
25     }

 

posted @ 2020-12-08 10:35  白兔兔秃~  阅读(171)  评论(0)    收藏  举报
/* */