2d swap between 2 player camers.

Hello everyone! I’m new to unity (and coding) so my coding skills and knowledge is very limited. I have set up a scene with 2 playable characters as well as a scrip to change between both of them by pressing the left alt key. I am currently trying to create a script that not only switches between both players but also their cameras. Sadly I have been unable to do this. I’d love some help/advice for the code and how to set up the camera to switch between the players. Thank You!

player movement script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
    private float movementInputDirection;

    private bool isFacingRight = true;
    private bool isWalking;
    private bool isRuning;
    private bool isGrounded;
    private bool canJump;

    private int amountOfJumpsLeft;

    private Rigidbody2D rb;
    private Animator anim;

    public int amountOfJumps = 1;

    public float movementspeed = 1.0f;
    public float jumpForce = 3.0f;
    public float groundCheckRadius;

    public Transform groundCheck;

    public LayerMask whatIsGround;

    // Start is called before the first frame update
    void Start()
    {

        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        amountOfJumpsLeft = amountOfJumps;

    }

    // Update is called once per frame
    void Update()
    {
        CheckInput();
        CheckMovementDirection();
        UptadeAnimations();
        CheckIfCanJump();

        if (Input.GetKey(KeyCode.LeftShift))
            movementspeed = 3f;
        else
            movementspeed = 1f;

        if (Input.GetKey(KeyCode.LeftShift))
            isRuning = true;
        else isRuning = false;
    }

    private void FixedUpdate()
    {
        ApplyMovemant();
        CheckSurroundings();
    }

    private void CheckSurroundings()
    {
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    }

    private void CheckIfCanJump()
    {
        if (isGrounded && rb.velocity.y <= 0)
        {
            amountOfJumpsLeft = amountOfJumps;
        }

        if (amountOfJumpsLeft <= 0)
        {
            canJump = false;
        }
        else
        {
            canJump = true;
        }
    }

    private void CheckMovementDirection()
    {
        if (isFacingRight && movementInputDirection > 0)
        {
            Flip();
        }
        else if (!isFacingRight && movementInputDirection < 0)
        {
            Flip();
        }

        if (rb.velocity.x != 0)
        {
            isWalking = true;
        }
        else
        {
            isWalking = false;
        }
    }

    private void UptadeAnimations()
    {
        anim.SetBool("isWalking", isWalking);
        anim.SetBool("isGrounded", isGrounded);
        anim.SetFloat("yVelocity", rb.velocity.y);
        anim.SetBool("isRuning", isRuning);
    }

    private void CheckInput()
    {
        movementInputDirection = Input.GetAxisRaw("Horizontal");
        if (Input.GetButtonDown("Jump"))
        {
            Jump();
        }
    }

    private void Jump()
    {
        if (canJump)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
        amountOfJumpsLeft--;

    }

    private void ApplyMovemant()
    {
        rb.velocity = new Vector2(movementspeed * movementInputDirection, rb.velocity.y);
    }

    private void Flip()
    {
        isFacingRight = !isFacingRight;
        transform.Rotate(0.0f, 180.0f, 0.0f);
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
    }
}

Player swap code:

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

public class Swicth : MonoBehaviour
{
    public playerMovement playerMovement;
    public playerMovement playerMovement2;
    public bool player1Active = true;
  

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftAlt))
        {
            SwitchPlayer();
        }
    }

    public void SwitchPlayer()
    {
        if (player1Active)
        {
            playerMovement.enabled = false;
            playerMovement2.enabled = true;
            player1Active = false;
        }
        else
        {
            playerMovement.enabled = true;
            playerMovement2.enabled = false;
            player1Active = true;
        }
    }
}

I’m going to just speak in general terms and let you translate to your code.

Break the processing of this into steps done each frame:

  • detect and record the user intent “I want to switch players!” Record that this happened in a bool.

  • process the detected intent separately:
    → toggle the boolean
    → clear this intent (it has been processed).

Now observe the player number boolean
Is it different than last frame? (keep track of what it was)
→ deactivate player 1 and turn off his camera
→ activate player 2 and turn on his camera
else: the other way around.

1 Like

Thank you for the advice. Unfortunately my computer is in the repair shop, so I’ll have to implement your suggestions later this week.

I have a question:

  • I currently have a main camera and one virtual camera for each character. Do i need to change that or…?

I will tell you how it goes once I’ve done it.