【问题标题】:Camera not working on mobile (Android)相机无法在移动设备上运行 (Android)
【发布时间】:2014-05-27 09:32:35
【问题描述】:

我正在使用 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) 上试用过

【问题讨论】:

    标签: android unity3d


    【解决方案1】:

    最简单的方法是在您的相机上附加一个脚本,让它跟随您的播放器。通过放置 public 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
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 2022-06-24
      • 2014-08-30
      • 2017-01-28
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多