Professional Professional
Мудрец
(11347)
1 месяц назад
Проблема заключается в том, что в методе Start() камера сразу перемещается к игроку, а не следует за ним. Это происходит потому, что в методе Start() вызывается метод FindPlayer(), который устанавливает позицию камеры на позицию игрока.
Чтобы исправить эту проблему, нужно изменить метод Start() так, чтобы он не перемещал камеру к игроку, а только находил его позицию. Затем, в методе Update(), нужно изменить позицию камеры на позицию игрока с помощью метода Lerp().
Измененный код будет выглядеть так:
using UnityEngine;
public class CameraFollow2D : MonoBehaviour
{
public float damping = 3f;
public Vector2 offset = new Vector2(2f, 1f);
public bool faceLeft;
private Transform player;
private int lastX;
void Start()
{
offset = new Vector2(Mathf.Abs(offset.x), offset.y);
FindPlayer(faceLeft);
}
public void FindPlayer(bool playerFaceLeft)
{
player = GameObject.FindGameObjectWithTag("Player").transform;
lastX = Mathf.RoundToInt(player.position.x);
}
void Update()
{
if (player)
{
int currentX = Mathf.RoundToInt(player.position.x);
if (currentX > lastX) faceLeft = false;
else if (currentX < lastX) faceLeft = true;
lastX = Mathf.RoundToInt(player.position.x);
Vector3 target;
if (faceLeft)
{
target = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
target = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}
Vector3 currentPosition = Vector3.Lerp(transform.position, target, damping * Time.deltaTime);
transform.position = currentPosition;
}
}
}
Теперь камера будет следовать за игроком, а не перемещаться к нему в начале игры.
Код:
using UnityEngine;
using System.Collections;
public class CameraFollow2D : MonoBehaviour {
public float damping = 3f;
public Vector2 offset = new Vector2(2f, 1f);
public bool faceLeft;
private Transform player;
private int lastX;
void Start ()
{
offset = new Vector2(Mathf.Abs(offset.x), offset.y);
FindPlayer(faceLeft);
}
public void FindPlayer(bool playerFaceLeft)
{
player = GameObject.FindGameObjectWithTag("Player").transform;
lastX = Mathf.RoundToInt(player.position.x);
if(playerFaceLeft)
{
transform.position = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
transform.position = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}
}
void Update ()
{
if(player)
{
int currentX = Mathf.RoundToInt(player.position.x);
if(currentX > lastX) faceLeft = false; else if(currentX < lastX) faceLeft = true;
lastX = Mathf.RoundToInt(player.position.x);
Vector3 target;
if(faceLeft)
{
target = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
target = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}
Vector3 currentPosition = Vector3.Lerp(transform.position, target, damping * Time.deltaTime);
transform.position = currentPosition;
}
}
}