简体   繁体   中英

Camera not working on mobile (Android)

I'm making a simple 2D game with Unity, nothing really hard to code, but I have a problem with moving the camera on mobile devices (in the editor it's working just fine!).

Here's the script that move the camera (custom smooth follow script. It's following the player and the damping variable is set to 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);     
    }
}

Here's the full project if someone want to try it (RAR archive, 10Mo)

ps: I tried it on a Samsung Galaxy S3 (i9300)

The easiest way to do this is attach a script to your camera that allows it to follow your player. Create a reference to the player by putting public Transform Player; then drag the player object from your scene into the field created in the inspector tab. Then watch as the camera will follow the 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

}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM