未完!
实现过程
创建场景
导入坦克资源场景
Lightmapping光照贴图技术是一种增强静态场景光照效果的技术,其优点是可以通过较少的性能消耗使静态场景看上去更加真实,丰富,更加具有立体感;缺点是不能用来实时地处理动态光照。当游戏场景包含了大量的多边形时,实时光源和阴影对游戏的性能的影响会很大。这时使用Lightmapping技术,将光线效果预渲染成贴图使用到多边形上模拟光影效果。
加载光照贴图有时候会造成场景卡顿
Auto Generate :设置成不自动生成
Generate Lighting :生成一次场景光照
坦克的前后移动
添加坦克预设体进入游戏场景,DustTrail粒子特效预设体放到坦克上
(GameObject ->Align With View 将Game视图对准当前Scene视图)
给坦克添加碰撞器和刚体
(碰撞器慢慢移,拖动数值调整到刚好包围完坦克)
添加移动坦克脚本
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movement : MonoBehaviour { public float speed = 5; // Use this for initialization void Start () { } // Update is called once per frame void Update () { //获得玩家的键盘输入 float v = Input.GetAxis("Vertical"); //让坦克前后移动 GetComponent<Rigidbody>().velocity = transform.forward * v * speed; } }
设置坦克移动速度
public float speed = 5;
获得玩家的键盘输入
float v = Input.GetAxis("Vertical");
让坦克前后移动
GetComponent<Rigidbody>().velocity = transform.forward * v * speed;
(将相机放到坦克里面,实现相机和坦克一起移动)
坦克的旋转
查看Unity输入按键值
水平移动:Vertical
左右移动:Horizontal
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movement : MonoBehaviour { public float speed = 10; public float angularSpeed = 5; // Use this for initialization void Start () { } // Update is called once per frame void Update () { //获得玩家的键盘输入 float v = Input.GetAxis("Vertical"); float h = Input.GetAxis("Horizontal"); //让坦克前后移动 GetComponent<Rigidbody>().velocity = transform.forward * v * speed; GetComponent<Rigidbody>().angularVelocity = transform.up * h * angularSpeed; } }
设置坦克移动转动角速度
public float angularSpeed = 5;
获得玩家的键盘输入
float h = Input.GetAxis("Horizontal");
让坦克前后移动
GetComponent<Rigidbody>().angularVelocity = transform.up * h * angularSpeed;
为实现双人坦克效果,提高按键灵活性,在InputManager管理器中添加两个Axes
修改Axes值Horizontal1、Horizontal2、Vertical1、Vertical2
我把玩家二上下左右WSAD按键修改为IKJL,实现双人笑游戏效果
默认角色是1号玩家
public string player = "1"; //获得玩家的键盘输入 float v = Input.GetAxis("Vertical"+player); float h = Input.GetAxis("Horizontal"+ player);
新建双人坦克修改Player值
坦克子弹A
坦克子弹B