zoukankan      html  css  js  c++  java
  • unity3d-代码控制游戏角色控制器移动

    先上一个gif看看效果。因为图片大小限制。所以录制的比较小。个人认为效果比较牵强。特别是里面的逻辑代码。 不过我还是认为一切是为了先实现,因为我是刚接触的新手。

    工程结构图

     这次实现的效果是:

     1:摄像机跟着角色移动,上篇博客说的是。把摄像机变成角色的子对象。发现没有我想要的效果。跟着角色移动代码是我自己弄出来的。不知道实际项目中是否也是这样。

     2:利用角色控制器组建 CharacterController 实现角色移动。

     3:当鼠左键击地形,角色自动寻路。今天发现用unity3d有自带的NavMesh实现自带寻路,不过还没去研究。

     4:当鼠左键点击地形。实现一个高亮效果。

     5:鼠标右键单击播放杀怪动画。

     6:到达关卡场景,进入下一关。本来是想杀boss后在进入,但血条不会弄。

     ..............

    Hierarchy视图中元素。分别是:主摄像机,平行光,地形,墙(也就是我这里的关卡),无效的元素,当前角色,鼠标点击显示图片,boss

    加载场景的时候。需要把加载的场景Build进去

     

    最后看看实现代码 ,有点牵强。也写了很多注释。方便大家和自己以后查看。

      1 using UnityEngine;
      2 using System.Collections;
      3 
      4 public class chartMove : MonoBehaviour
      5 {
      6 
      7     public float speed = 5f;
      8     public GameObject cursor;//鼠标点击光标显示图片
      9     public GameObject boos; //boos对象
     10 
     11 
     12     private Vector3 targetPos; //目标位置
     13     private Vector3 mousePos; //鼠标单击的位置
     14     private Vector3 beforePos;//角色移动前的位置
     15     private CharacterController charController = null;
     16     private bool cursorState = false; //当前是否点击了鼠标
     17     private bool BossIsDie;//bos是否over
     18 
     19     // Use this for initialization
     20     void Start()
     21     {
     22         charController = GetComponent<CharacterController>();
     23         targetPos = transform.position;
     24         mousePos = transform.position;
     25     }
     26 
     27     // Update is called once per frame
     28     void Update()
     29     {
     30         beforePos = transform.position; //保存当前的位置
     31 
     32         //获取按键轴
     33         /*
     34          * Horizontal-->对应键盘上上的A,D
     35          * Vertical-->对应键盘上上的W,S
     36          * 当没有按键的时候为0 。
     37          * 其值分别为 -1(左,下) 0 1(上。右)
     38          */
     39         float x = Input.GetAxis("Horizontal");
     40         float y = Input.GetAxis("Vertical");
     41 
     42         //transform.Translate(new Vector3(x, 0, y) * Time.deltaTime * speed,Space.World);
     43 
     44         //if (x == 0 && y == 0 && !cursorState)
     45         //{
     46         //    transform.animation.Play("idle");
     47         //}
     48         if (x != 0 || y != 0)
     49         {
     50             transform.animation.Play("run"); //奔跑
     51             cursor.SetActive(false);
     52             //charController.Move(new Vector3(x, 0, y) * Time.deltaTime * speed);
     53             //transform.Translate(new Vector3(x, 0, y) * Time.deltaTime * speed, Space.World);
     54             cursorState = false;
     55             charController.SimpleMove(new Vector3(x, 0, y) * speed);
     56 
     57             //设置角色面朝的方向
     58             Vector3 lookPos = transform.position - beforePos + transform.position;
     59             transform.LookAt(lookPos);
     60         }
     61         else
     62         {
     63 
     64         }
     65 
     66 
     67         //左键选择路径
     68         if (Input.GetMouseButtonDown(0))
     69         {
     70             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     71             RaycastHit hit;
     72             if (Physics.Raycast(ray, out hit, 100)) //100为射线的长度,限制用户瞎点击。在100以为点击无效
     73             {
     74                 if (hit.collider.name == "Terrain")
     75                 {
     76                     mousePos = hit.point;
     77                     Vector3 lookPos = mousePos - transform.position + transform.position;
     78                     transform.LookAt(lookPos);
     79                     cursor.SetActive(true); //显示图片:图片一开始是隐藏的。
     80                     cursor.transform.position = mousePos;
     81                     cursorState = true;
     82                 }
     83             }
     84         }
     85 
     86 
     87         //判断角色是否到达目的地
     88         int distance = (int)Vector3.Distance(mousePos, transform.position);
     89 
     90         //这个1.08的临界值。我是根据调试得来的。具体为什么是1.08,暂时还不知道
     91         if (distance > 1.08f && cursorState)
     92         {
     93             transform.animation.Play("run"); //奔跑
     94 
     95             //控制向量长度在 speed 范围在内
     96             Vector3 setp = Vector3.ClampMagnitude(mousePos - transform.position, speed);
     97             //charController.Move(setp*0.3f); //开始我用这个方法。不是我想要的效果
     98             /*
     99               这里要区别于:Move() 一个更加复杂的运动函数,每次都绝对运动
    100              * SimpleMove:以一定的速度移动角色
    101              */
    102             //cursorState = false;
    103             charController.SimpleMove(setp);
    104 
    105         }
    106         else
    107         {
    108             if (x == 0 && y == 0)
    109             {
    110                 //transform.animation.Play("idle");
    111                 cursor.SetActive(false);
    112                 //cursorState = false;
    113                 if (!transform.animation.IsPlaying("attack"))//当前没有攻击
    114                     transform.animation.Play("idle");
    115             }
    116             //transform.animation.Play("idle");
    117         }
    118         //右键攻击
    119         /*
    120          GetMouseButton:按下不弹起就一直执行
    121          * GetMouseButtonDown:按下执行一次。
    122          * 
    123          */
    124         if (Input.GetMouseButton(1))
    125         {
    126             cursorState = true;
    127             transform.animation.Play("attack");//攻击
    128         }
    129         //transform.animation.Play("run"); //奔跑
    130         //设置角色面朝的方向
    131         //Vector3 lookPos = transform.position - beforePos + transform.position;
    132         //transform.LookAt(lookPos);
    133 
    134 
    135         //设置相机跟随
    136         Vector3 camera = Camera.main.transform.position; //获取主相机的位置
    137         Vector3 currChart = transform.position;//当前角色位置
    138 
    139         camera.x = currChart.x;
    140         camera.y = currChart.y + 5;
    141         camera.z = currChart.z - 10;
    142 
    143         Camera.main.transform.position = camera;
    144 
    145         //if ((charController.collisionFlags & CollisionFlags.Above) != 0)
    146         //    print("touched the ceiling");
    147 
    148     }
    149     /// <summary>
    150     /// 当控制器碰撞一个正在运动的碰撞器时,OnControllerColliderHit 被调用。
    151     /// </summary>
    152     /// <param name="hit"></param>
    153     public void OnControllerColliderHit(ControllerColliderHit hit)
    154     {
    155 
    156         //如果碰到的是墙并且boos is die,进入下一关,这里 墙 代替关卡   
    157         if (hit.transform.name == "wall")
    158         {
    159             //加载关卡,2 表示Build Settings中场景的索引
    160             Application.LoadLevel(2);
    161         }
    162     }
    163 
    164     public void OnTriggerEnter(Collider other)
    165     {
    166 
    167     }
    168 
    169 }

    下一篇想弄动画的剪切,因为美工给程序的动画是一个总体。我们需要把某一个动作。比如蹦跑,杀怪等单个动画弄出来播放,就像上面的

    transform.animation.Play("attack");//攻击
  • 相关阅读:
    (转)android res文件夹里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
    ListView模拟微信好友功能
    最小生成树(普利姆算法、克鲁斯卡尔算法)
    Android Afinal框架学习(二) FinalActivity 一个IOC框架
    浅谈WebLogic和Tomcat
    变量定义和声明的差别(整理)
    浏览器内核分类
    设计模式 之 享元
    FPGA中浮点运算实现方法——定标
    兔子--Android中的五大布局
  • 原文地址:https://www.cnblogs.com/niboy/p/4218993.html
Copyright © 2011-2022 走看看