今天首先完善了昨天的小僵尸,然后开始做火箭鼠(老师所谓“第一个丑陋的完整小游戏”hhhhh,后天继续!)
任务一:接昨天小僵尸行走动作完善:
昨天把小僵尸做到了一:在原地把分解动作图片连起来动,二:随鼠标点击的方向移动过去。
仍存在的问题:一:鼠标点击只是确定了移动方向而不是末位置,小僵尸会朝那个方向一直走过去“不撞南墙不停”。
二:小僵尸的头以及身体朝向不会变化,有的方向会出现“螃蟹走”。
今天首先解决了第二个问题,利用到的新知识有弧度角度加入:(第一个问题并没有解答,要记得!)
using UnityEngine;
using System.Collections;
public class weizhi : MonoBehaviour {
public float movespeed; // this is units
public float turnspeed; // move your head
private Vector3 moveDirection;
// Use this for initialization
void Start ()
{
moveDirection = Vector3.right;
}
// Update is called once per frame
void Update () {
Vector3 currentPostion = transform.position;
if(Input.GetButton("Fire1")) //自己改为方向键操控
{ Vector3 moveToward = Camera.main.ScreenToWorldPoint(Input.mousePosition);//主摄像机view
moveDirection = moveToward - currentPostion; //
moveDirection.z = 0; //because of 2D
moveDirection.Normalize(); //all move
}
Vector3 targetPosition = currentPostion + moveDirection;// * movespeed;
transform.position = Vector3.Lerp(currentPostion,
targetPosition,
Time.deltaTime); //Lerp:差值(zhi (jia zhi)) let it stop some
float targetAngle = Mathf.Atan2(moveDirection.y,
moveDirection.x) * Mathf.Rad2Deg;//control the foot (jiao du)(Rad to Deg从弧度转为角度)
transform.rotation = Quaternion.Slerp(transform.rotation,//最后位置
Quaternion.Euler(0,0,targetAngle),//targetAngle所求角度,
turnspeed * Time.deltaTime); //Slerp也是差值,but not only ,左边变量是过程
}
}
任务二:火箭鼠是个大工程
(图层、喷气效果、鼠标/键盘操控飞动、飞过一张又一张又一张又..那么背景图怎么设置、工程的美观性一部分体现在规范划分文件夹、、及时命名新建文件以便各自区分、代码编写中使用有意义的词汇命名以增加可读性)
今天仅写脚本部分吧:
//第一个脚本文件(火箭鼠位置飞动)
using UnityEngine;
using System.Collections;
public class mousecontrol : MonoBehaviour {
public float jetpackFore = 75.0f; //定义一个力
public float movespeed = 5.0f;
void Start () {
}
void Update () {
}
void FixedUpdate() //区别:上者不是按照固定时间间隔,而这个是。
{
bool ForceActive = Input.GetKeyDown(KeyCode.W);//设置鼠标操作,所以定义一个布尔型
bool ForceActive1 = Input.GetKeyDown(KeyCode.D);
bool ForceActive2 = Input.GetKeyDown(KeyCode.A);
if (ForceActive)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0,jetpackFore));//Rigidbody2D是与你首页增加的那个钢体名字一致,这是操作钢体的
}
if (ForceActive1)
{
Vector2 newVelocity = GetComponent<Rigidbody2D>().velocity; //想要人物动也就是钢体动,所以获取钢体
newVelocity.x = movespeed;
GetComponent<Rigidbody2D>().velocity = newVelocity;
}
if (ForceActive2)
{
Vector2 newVelocity = GetComponent<Rigidbody2D>().velocity; //想要人物动也就是钢体动,所以获取钢体
newVelocity.x = - movespeed;
GetComponent<Rigidbody2D>().velocity = newVelocity;
}
}
}
//第二个脚本文件:摄像机移动(功能:使火箭鼠不会飞出屏幕)
using UnityEngine;
using System.Collections;
public class camera : MonoBehaviour {
public GameObject targetobject; //the position of mouse
private float distancetoTarget;//just for the first position
// Use this for initialization
void Start () {
distancetoTarget = transform.position.x - targetobject.transform.position.x;//targetobject is mouse
}
// Update is called once per frame
void Update () {
float targetobjectX = targetobject.transform.position.x;
Vector3 newCameraPosition = transform.position;//数字代表几维
newCameraPosition.x = targetobjectX + distancetoTarget; //distancetoTarget是为了使mouse始终在屏幕的某个位置,通过改变摄像机实现了,而不是mouse
transform.position = newCameraPosition;//这一步:赋值回去使执行
}
}
话外:左侧任何添加GameObject (空白块)都应当时重命名,因为随着文件越来越多 容易混淆
预制件Prefabs(随着飞鼠飞出去,当场景没了,自动随机填充)
增加房间,写脚本如下:
//第三个脚本文件:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;//增加一个头文件,Generic(命名空间),List类 //for room ,链表
public class Room : MonoBehaviour {
public GameObject[] availableRooms; //可用的房间的数组
public List<GameObject> currentRooms;
private float screenWidthInPoints;//is当前屏幕视图范围。判定何时删掉左边房间,并且加上房间
void Start ()
{
float height = 2.0f * Camera.main.orthographicSize;//获取当前屏幕的高度,单位不是像素而是units,*它的宽高比(宽度/高度)
screenWidthInPoints = height * Camera.main.aspect; //以上获取了长宽比 ,注意都是针对屏幕而不是背景图尺寸什么的
} //以上是获取当前屏幕的宽度 --he says
void addRoom(float farRoomID)//增加房间
{//farRoomID最远的房间,也就是最右侧的,对应着,同时删除的当然也就是最左边的
int randomRoomIndex = Random.Range(0,availableRooms.Length); //确定了room的选择范围 //创建一个随机数Random.Range,最小值0 最大length
GameObject room = (GameObject)Instantiate(availableRooms [randomRoomIndex]);//(GameObject)强制转换//Instantiate初始化
//以下完成拼接
float roomWidth = room.transform.FindChild("floor").localScale.x;//当前房间的宽以地板碰撞体"floor"宽度代替了,所以要求它宽和房间宽相同//FindChild子。。"floor"和你的具体重命名一致
float roomCenter = roomWidth * 0.5f + farRoomID; //房间的中心位置roomCenter = roomWidth * 0.5f //首先得到它房间的一半。+ farRoomID是另外一个房间多出来的部分
room.transform.position = new Vector3 (roomCenter,0,0);//定义房间,然后拿上面值来用
currentRooms.Add(room); //Add为默认函数
}
void IsGeneratorRoom()
{
List<GameObject> DeleteRoom = new List<GameObject>();//保存将要删掉的房间:当它不在屏幕内才能删掉
bool addRooms = true;//判断是否增加房间
float playerX = transform.position.x;//获取飞鼠的位置的X轴
float DeleteRoomX = playerX - screenWidthInPoints;//坐标计算:飞鼠位置-当前屏幕宽度 = 不出现在屏幕的,将要删的宽度
//飞鼠的左侧空间够不够用
float AddRoomX = playerX + screenWidthInPoints; //和上行相对应 //飞鼠的右侧空间够不够用
float farRoomIndex = 0;
foreach (var room in currentRooms)//判断房间是否在**里//判断哪些房间需要删除
{
float roomWidth = room.transform.FindChild("floor").localScale.x;
float roomStartX = room.transform.position.x - roomWidth * 0.5f; //最左侧
float roomEndX = roomStartX + roomWidth; //最右侧位置
//下面是判定的核心环节 (用if语句:
if(roomStartX > AddRoomX)
addRooms = false;
if(roomEndX < DeleteRoomX)
DeleteRoom.Add(room);
farRoomIndex = Mathf.Max(farRoomIndex,roomEndX); //知道了新增加的房间的需要的长度 //两个相对位置
}
foreach (var room in DeleteRoom) //在链表里把这个房间彻底删掉
{
currentRooms.Remove(room); //其实上下为数据结构知识
Destroy(room);
}
if (addRooms)
{
addRoom(farRoomIndex);
}
}
void FixedUpdate()
{
IsGeneratorRoom();
}
void Update ()
{ }
}