Unity Multiplayer: Minimap Movement

Hello,

When I move on the minimap, it block my camera movement like if the restriction zone changed. It’s really annoying for the player because you could get stuck in a little square just because he used the Minimap once to move.

I tried to adapt my code several times by changing little details I still don’t see how the Minimap script is influencing the Camera Movement that much.

Here’s the Minimap code :

public class Minimap : MonoBehaviour, IPointerDownHandler, IDragHandler

{

[SerializeField] private RectTransform minimapRect = null;

[SerializeField] private float mapScale = 20f;

[SerializeField] private float offset = -7f;

private Transform playerCameraTransform;

private void Update()

{

    if(playerCameraTransform != null) { return; }

    if(NetworkClient.connection?.identity == null) { return; }

    playerCameraTransform = NetworkClient.connection.identity.GetComponent<LDMPlayer>().GetCameraTransform();

}

public void OnPointerDown(PointerEventData eventData)

{

    MoveCamera();

}

public void OnDrag(PointerEventData eventData)

{

    MoveCamera();

}

private void MoveCamera()

{

    Vector2 mousePos = Mouse.current.position.ReadValue();

    if(!RectTransformUtility.ScreenPointToLocalPointInRectangle(minimapRect, mousePos, null, out Vector2 localPoint)) { return;}

    Vector2 lerp = new Vector2((localPoint.x - minimapRect.rect.x)/minimapRect.rect.width, (localPoint.y-minimapRect.rect.y) /minimapRect.rect.height);

    Vector3 newCameraPos = new Vector3(Mathf.Lerp(-mapScale, mapScale, lerp.x), playerCameraTransform.position.y, Mathf.Lerp(-mapScale, mapScale, lerp.y));

    playerCameraTransform.position = newCameraPos + new Vector3 (0f, 0f, offset);

}

}

And here’s the Camera Movement script (I change it because the one from the course was absolutly not working and I had one working for a prvious game that I adapted for this one) :

public class CameraController : NetworkBehaviour

{

[SerializeField] private Transform playerCameraTransform = null;

public float panSpeed = 20f;

public float panBorderThickness = 10f;

public Vector3 panLimit;

public float scrollSpeed = 20f;

public float minY = 10f;

public float maxY = 120f;

public override void OnStartAuthority()

{

    playerCameraTransform.gameObject.SetActive(true);

}

public Transform GetTransform()

{

    return playerCameraTransform;

}

[ClientCallback]

private void Update()

{

    if (!hasAuthority || !Application.isFocused) { return; }

    UpdateCameraPosition();

}

private void UpdateCameraPosition()

{

    Vector3 position = transform.position;

    if (Input.GetKey("z") || Input.mousePosition.y >= Screen.height - panBorderThickness)

    {

        position.z += panSpeed * Time.deltaTime;

    }

    if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)

    {

        position.z -= panSpeed * Time.deltaTime;

    }

    if (Input.GetKey("q") || Input.mousePosition.x <= - panBorderThickness)

    {

        position.x -= panSpeed * Time.deltaTime;

    }

    if (Input.GetKey("d") || Input.mousePosition.x >= Screen.width - panBorderThickness)

    {

        position.x += panSpeed * Time.deltaTime;

    }

    float scroll = Input.GetAxis("Mouse ScrollWheel");

    position.y -= scroll * scrollSpeed * 100f * Time.deltaTime;

    position.x = Mathf.Clamp(position.x, -panLimit.x, panLimit.x);

    position.z = Mathf.Clamp(position.z, -panLimit.z, panLimit.z);

    position.y = Mathf.Clamp(position.y, minY, maxY);

    transform.position = position;

}

}

Thanks in advance,

Fabvison

Hi there,
So it’s behaving as if the mini-map script is affecting the camera controller script?

If this is the case, my suspicion is that it has something to do with the cameras or mouse positions, since those are the only shared objects between the two scripts.

Would you be able to share a video of the behaviour? That might help narrow it down.

Hello,

Thanks for your answer. Here’s a video :

I start at the bottom left corner and can moove there (you can see my camero position on the right. As I click on the minimap more in the center, I can’t go back in the corner by moving with the camera controller, I can only go up. And at the end, I reclick on the Minimap to go back in the original corner because it is the only way I can go back there.

The warnings on the log are there because I’m redoing the whole graphics, units and stuff, so the scripts are missing some references. But I have no message relative to the camera or the minimap and that bug appear before that so don’t pay attention to the log.

I was thinking about the camera position as a possible conflict between the two but the values of the limits for the camera controller don’t seem to change in the inspector while testing so I don’t really get why he’s stuck.

Thanks in advance,

Fabvison

So none of these values are changing at runtime? They are all fixed?

Try debugging your script and seeing where the camera movement is getting stalled.
Add Debug.logs after all the exit statements.

Hello,

Thanks a lot for your help, I actually found out the problem : I was updating the player position and not the playercameratransform.position. But my minimap was moving the playercameratransform.position. So, ofc, when I was in the limit of the map with the player and I was moving with the minimap, my player get stuck because he don’t update his position and think he is still on the border. So I just simply change the “transform. position” by “playercameratransform.position” and it’s fixed.

Have a nice day !
Fabvison

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms