我正在使用Unity制作一个简单的2D游戏,没有什么比编码更难,但是移动时我遇到了问题。 移动设备上的摄像头(在编辑器中工作得很好!)。
这是移动相机的脚本(自定义平滑跟随脚本。它跟随播放器,阻尼变量设置为10)。
using UnityEngine;
using System.Collections;
public class SmoothFollow : MonoBehaviour {
public Transform target; // The target we are following
public float positionDamping;
private Vector3 deltaPos;
// Use this for initialization
void Start () {
deltaPos = new Vector3(-4f, 1f, 0f);
}
// Update is called once per frame
void Update () {
}
public void FixedUpdate () {
Vector3 targetPosition = target.position - (Vector3.forward * 10);
//transform.position = Vector3.MoveTowards(transform.position, targetPosition + deltaPos, positionDamping * Time.deltaTime);
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, targetPosition + deltaPos, positionDamping * Time.deltaTime);
}
}
如果有人,这是完整的项目 想试试(RAR archive, 10Mo)
ps:我在三星Galaxy S3(i9300)上试过它
答案 0 :(得分:0)
最简单的方法是在相机上附加一个脚本,以便它可以跟随您的播放器。通过放置公共Transform Player创建对玩家的引用;然后将播放器对象从场景拖动到“检查器”选项卡中创建的字段中。然后观看相机将跟随播放器。
using UnityEngine; using System.Collections;
public class CameraFollow : MonoBehaviour {
public Transform Player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 playerpos = new Vector3 (Player.position.x, Player.position.y, -14); // the camera is fixed on the z-axis to maintain a distance of 14 to the player
transform.position = Vector3.Lerp(transform.position, playerpos, 5); // the "5" represents the speed at which the camera will move
}
}