1
\$\begingroup\$

I am developing simple 2D game in unity. I am locked with the camera movement for last two days. I've camera script that is working as follows:

  • Follow the player to X and Y direction
  • If player stops moving, Camera must find the nearest enemy and should move to that direction

Both of the above function works fine, but separately.

Camera finds and follows the enemy, when there is no input from player for last 10 seconds.

But after few seconds (When stops following enemy), Camera retains the player's position. That creates unlikely effect in the scene. I want the camera to have a last position and started following player from there.

For your reference I'm writing my Camera Script.

public class MultipleTargetCamera : MonoBehaviour {   public List<Transform> targets;
[ReadOnly]
public GameObject closestEnemy = null;

public EnemyController closestEnemyController = null;

public Vector3 offset;
public float smoothTime = 0.5f;

private Vector3 velocity;

Vector3 camPosition;

// Update is called once per frame
void LateUpdate()
{
    /* if (targets.Count == 0)
         return;*/
    if (!TheGlobals.instance.movingTowardsEnemy)
    {
        MoveCamera();
    }
    else
    {
        MoveAsEnemy();
    }
}

void MoveAsEnemy()
{
    camPosition = new Vector3(transform.position.x + closestEnemyController.xDiff,
            transform.position.x + closestEnemyController.yDiff,
            transform.position.z);
    transform.position = Vector3.SmoothDamp(transform.position, camPosition, ref velocity, smoothTime);
}

void MoveCamera()
{
    Vector3 centerPoint = GetCenterPoint();

    Vector3 newPosition = centerPoint + offset;

    transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, smoothTime);
}

Vector3 GetCenterPoint()
{
    Vector3 targetPos= new Vector3(targets[0].position.x,
        targets[0].position.y,
        transform.position.z);

    if(targets.Count==1)
    {
        return targetPos;
    }

    var bounds = new Bounds(new Vector3(targets[0].position.x,
        targets[0].position.y,
        transform.position.z), Vector3.zero);

    for(int i=0;i<targets.Count;i++)
    {
        bounds.Encapsulate(new Vector3(targets[i].position.x,
        targets[i].position.y,
        transform.position.z));
    }

    return bounds.center;
}

public void FindNearestEnemy()
{
    GameObject[] gos;
    gos = GameObject.FindGameObjectsWithTag("Enemy");

    float distance = Mathf.Infinity;
    Vector3 position = transform.position;
    foreach (GameObject go in gos)
    {
        Vector3 diff = go.transform.position - position;
        float curDistance = diff.sqrMagnitude;
        if (curDistance < distance)
        {
            closestEnemy = go;
            distance = curDistance;
        }
    }
    closestEnemyController = closestEnemy.GetComponent<EnemyController>();
    targets[0].parent = this.transform;
    StartCoroutine(StopMovingTowardsEnemy());
}

IEnumerator StopMovingTowardsEnemy()
{
    yield return new WaitForSeconds(0.01f);
    TheGlobals.instance.movingTowardsEnemy = false;
}

}

I hope I did explain well. Looking for the answer. Thank you in advance!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.