[待补充]>>>emm录制gif的时候出错了…
FFFFFlipping-V1.1.apk
项目中的使用场景
拓展:
效果图:
实现细节:
Mathf.Lerp
插值实现关键代码如下:
using UnityEngine;
public class CameraMovement : MonoBehaviour {
public GameObject followTarget;
public float moveSpeed;
void Update() {
if (followTarget != null) {
//相机位置Z值与目标点的Z值做插值, 实现相机前后跟随, 而目标点运动不影响
var newZ = Mathf.Lerp(transform.position.z, followTarget.transform.position.z, Time.deltaTime * moveSpeed);
var newVector3 = new Vector3(transform.position.x, transform.position.y, newZ);
transform.position = newVector3;
}
}
}
效果图:
关键代码如下:
using UnityEngine;
public class BlockMovement : MonoBehaviour {
void OnTriggerExit(Collider other) {
//当相机看不见某一行block时, 将该block移到最后一排, 实现无限地图
if (other.gameObject.tag == "Floor") {
BlockManager.Instance.ChangeBlockPosition(other.transform);
}
if (other.gameObject.tag == "PoolItem") {
var poolItem = other.transform.GetComponent<Cell>();
poolItem.ReleaseIntoPool();
}
}
}
敌人看到主角时, 会转向主角
怎样算看到呢? 项目中, 主角与敌人的z值距离小于指定值时, 可以断定主角进入敌人的视觉范围
要怎么转向主角呢? 用四元数来表示旋转, 同时限制欧拉角中的y值, 来限制旋转角度
关键代码如下:
protected virtual bool SeesPlayer =>
((Player != null) && (Mathf.Abs((int)(transform.position.z - Player.transform.position.z)) <= visionRange));
//...
protected virtual void Update() {
if (Player != null) {
if (SeesPlayer) TurnTowardPlayer();
}
}
//...
private void TurnTowardPlayer() {
Vector3 position = Player.transform.position;
//忽略y值的影响
position.y = transform.position.y;
Vector3 forward = transform.position - position;
if (forward != Vector3.zero) {
Quaternion b = Quaternion.LookRotation(forward);
transform.rotation = Quaternion.Lerp(transform.rotation, b, Time.deltaTime * turnRate);
float y = transform.eulerAngles.y;
//确定旋转角度
if (y > 180f) y -= 360f;
//将旋转角度限制在-45度和+45度之间
transform.rotation = Quaternion.Euler(0f, Mathf.Clamp(y, -mLookLimit, mLookLimit), 0f);
}
}
拓展:
效果图为:
关键代码为:
///
/// 左右跳时, 主角也向前跳
///
/// -1表示向左跳, 1表示向右跳
private void ForwardJump(int direction) {
mCurrentX += direction;
if (direction == -1) {
//越界了
if (mCurrentX < 0) {
mCurrentX = 2;
var newVector3 = transform.position;
newVector3.x = 2 * characterPositionOffset;
transform.position = newVector3;
}
transform.Translate(Vector3.left * characterPositionOffset);
}
if (direction == 1) {
//越界了
if (mCurrentX > 2) {
mCurrentX = 0;
var newVector3 = transform.position;
newVector3.x = -2 * characterPositionOffset;
transform.position = newVector3;
}
transform.Translate(Vector3.right * characterPositionOffset);
}
JumpForward();
}
public void LowBlood() {
Time.timeScale = 0.5f;
lowBloodView.SetActive(true);
}
public void ResumeNormalBlood() {
Time.timeScale = 1;
lowBloodView.SetActive(false);
}
敌人AI, 项目当中暂时没有实现AI功能. 后期有机会我将会补上.
参考:
纯属因为个人喜欢的两款放荡不羁的游戏
(1)Eggggg(喷蛋狂人)
(2)Flipping Legend (弹跳传奇)
CSDN: jingangxin36
jingangxin36/FFFFFlipping