0
\$\begingroup\$

After my character spawns, the camera just stays in the same place. How can I solve that?

Here's my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SideScrolling : MonoBehaviour
{
    private Transform mario;

    private void Start ()
    {
        mario = GameObject.FindWithTag("Player").transform;
    }


    private void LateUpdate()
    {
        Vector3 cameraPosition = transform.position; 
        cameraPosition.x = Mathf.Max(cameraPosition.x, mario.position.x);
        transform.position = cameraPosition;
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Do you see any errors in the console window? And are you moving your character to the left or right? Do you notice this issue from the beginning of the scene, or only after the character has respawned? \$\endgroup\$
    – DMGregory
    Commented Jan 12 at 1:51
  • \$\begingroup\$ theres no errors, the character respawns like normal but the camera stays in place \$\endgroup\$ Commented Jan 12 at 2:00

1 Answer 1

0
\$\begingroup\$

Change

cameraPosition.x = Mathf.Max(cameraPosition.x, mario.position.x);    

To

cameraPosition.x = mario.position.x;    

Look at the following code

Vector3 cameraPosition = transform.position; 
cameraPosition.x = Mathf.Max(cameraPosition.x, mario.position.x);
transform.position = cameraPosition;

If cameraPosition.x >= mario.position.x, then the camera won't move if mario is moving toward negative X direction, because Mathf.Max(cameraPosition.x, mario.position.x) would return the value of cameraPosition.x.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .