tilemap的demo脚本,大概注释一下
tk2dTileMapDemoPlayer.cs主要是控制玩家的行动和检测金币的碰撞,及时处理文字。
using UnityEngine;
using System.Collections;
public class tk2dTileMapDemoPlayer : MonoBehaviour {
public tk2dTextMesh textMesh;//得分
public tk2dTextMesh textMeshLabel;//score
Vector3 textMeshOffset;//装B,不用附属关系,手动保存偏移量,用来设置坐标
bool textInitialized = false;
public float addForceLimit = 1.0f;
public float amount = 500.0f;
public float torque = 50;
tk2dSprite sprite;
int score = 0;
float forceWait = 0;
float moveX = 0.0f;
bool AllowAddForce { get { return forceWait < 0.0f; } }
void Awake() {
sprite = GetComponent<tk2dSprite>();
if (textMesh == null || textMesh.transform.parent != transform) {
Debug.LogError("Text mesh must be assigned and parented to player.");
enabled = false;
}
textMeshOffset = textMesh.transform.position - transform.position;
textMesh.transform.parent = null;
//上面这几行就是装B,它一定要确保textmesh的父物体是玩家,然后保存textmesh和player坐标的偏移量后,把textmesh的父物体设置为空。
textMeshLabel.text = "instructions";
textMeshLabel.Commit();
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer ||
Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.OSXDashboardPlayer) {
textMesh.text = "LEFT ARROW / RIGHT ARROW";
}
else {
textMesh.text = "TAP LEFT / RIGHT SIDE OF SCREEN";
}
textMesh.Commit();
//上面几行是设置提示信息
Application.targetFrameRate = 60;
}
void Update() {
forceWait -= Time.deltaTime;
//根据是否等待状态设置精灵的图像,绿色和红色
string spriteName = AllowAddForce ? "player" : "player_disabled";
if (sprite.CurrentSprite.name != spriteName) {
sprite.SetSprite(spriteName);
}
if (AllowAddForce) {
float x = 0;
if (Input.GetKeyDown(KeyCode.RightArrow)) x = 1;
else if (Input.GetKeyDown(KeyCode.LeftArrow)) x = -1;
for (int i = 0; i < Input.touchCount; ++i) {
if (Input.touches[i].phase == TouchPhase.Began) {
x = Mathf.Sign(Input.touches[i].position.x - Screen.width * 0.5f);
break;
}
}
if (x != 0) {
// make sure text meshes are changed on first button press / touch
//这里是按下控制键后,消去提示信息,只执行一次。
if (!textInitialized) {
textMeshLabel.text = "score";
textMeshLabel.Commit();
textMesh.text = "0";
textMesh.Commit();
textInitialized = true;
}
// The actual applying of force is deferred to the next FixedUpdate for predictable
// physics behaviour
moveX = x;
}
}
textMesh.transform.position = transform.position + textMeshOffset;
}
//处理Rigidbody时,需要用FixedUpdate代替Update
void FixedUpdate () {
if (AllowAddForce && moveX != 0) {
forceWait = addForceLimit;
rigidbody.AddForce(new Vector3(moveX * amount, amount, 0) * Time.deltaTime, ForceMode.Impulse);//施加力
rigidbody.AddTorque(new Vector3(0,0,-moveX * torque) * Time.deltaTime, ForceMode.Impulse);//施加力矩,使player绕z轴旋转。
moveX = 0;
}
}
void OnTriggerEnter(Collider other) {
Destroy( other.gameObject );
score++;
textMesh.text = score.ToString();
textMesh.Commit();
}
}
tk2dTileMapDemoFollowCam.cs主要是跟随玩家,并达到平滑缩放的效果
using UnityEngine;
using System.Collections;
public class tk2dTileMapDemoFollowCam : MonoBehaviour {
tk2dCamera cam;
public Transform target;
public float followSpeed = 1.0f;
public float minZoomSpeed = 20.0f;
public float maxZoomSpeed = 40.0f;
public float maxZoomFactor = 0.6f;
void Awake() {
cam = GetComponent<tk2dCamera>();
}
void FixedUpdate() {
Vector3 start = transform.position;
//摄像头坐标向玩家坐标移动
Vector3 end = Vector3.MoveTowards(start, target.position, followSpeed * Time.deltaTime);
end.z = start.z;
transform.position = end;
if (target.rigidbody != null && cam != null) {
//根据player的速度向量,生成一个0~1之间的因子,用线性插值的方法确定缩放率,达到平滑过渡视野的效果,MoveTowards效果实际上和 Mathf.Lerp相同。
float spd = target.rigidbody.velocity.magnitude;
float scl = Mathf.Clamp01((spd - minZoomSpeed) / (maxZoomSpeed - minZoomSpeed));
float targetZoomFactor = Mathf.Lerp(1, maxZoomFactor, scl);
cam.ZoomFactor = Mathf.MoveTowards(cam.ZoomFactor, targetZoomFactor, 0.2f * Time.deltaTime);
}
}
}
tk2dTileMapDemoCoin.cs就是金币不在摄像头视野时不工作,提高效率。
通过这三个脚本,我大概学习了player的行为控制,之前我都是直接改transform,导致刚体之间可以穿过;摄像头平常我也只是简单的用lookat来实现跟随,当然这看情况。在rpg游戏中都是这样普通跟随,比如dnf的城镇移动和副本移动,只有在特殊的镜头才会缩放。在太空游戏中,我就觉得缩放效果会更好一点,飞船加速时预览更大的地图,游览星际。