Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question 2d swap between 2 player camers.

Discussion in '2D' started by Mbarocks77, Sep 18, 2022.

  1. Mbarocks77

    Mbarocks77

    Joined:
    Aug 14, 2020
    Posts:
    4
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class playerMovement : MonoBehaviour
    5. {
    6.     private float movementInputDirection;
    7.  
    8.     private bool isFacingRight = true;
    9.     private bool isWalking;
    10.     private bool isRuning;
    11.     private bool isGrounded;
    12.     private bool canJump;
    13.  
    14.     private int amountOfJumpsLeft;
    15.  
    16.     private Rigidbody2D rb;
    17.     private Animator anim;
    18.  
    19.     public int amountOfJumps = 1;
    20.  
    21.     public float movementspeed = 1.0f;
    22.     public float jumpForce = 3.0f;
    23.     public float groundCheckRadius;
    24.  
    25.     public Transform groundCheck;
    26.  
    27.     public LayerMask whatIsGround;
    28.  
    29.     // Start is called before the first frame update
    30.     void Start()
    31.     {
    32.  
    33.         rb = GetComponent<Rigidbody2D>();
    34.         anim = GetComponent<Animator>();
    35.         amountOfJumpsLeft = amountOfJumps;
    36.  
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         CheckInput();
    43.         CheckMovementDirection();
    44.         UptadeAnimations();
    45.         CheckIfCanJump();
    46.  
    47.         if (Input.GetKey(KeyCode.LeftShift))
    48.             movementspeed = 3f;
    49.         else
    50.             movementspeed = 1f;
    51.  
    52.         if (Input.GetKey(KeyCode.LeftShift))
    53.             isRuning = true;
    54.         else isRuning = false;
    55.     }
    56.  
    57.     private void FixedUpdate()
    58.     {
    59.         ApplyMovemant();
    60.         CheckSurroundings();
    61.     }
    62.  
    63.     private void CheckSurroundings()
    64.     {
    65.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    66.     }
    67.  
    68.     private void CheckIfCanJump()
    69.     {
    70.         if (isGrounded && rb.velocity.y <= 0)
    71.         {
    72.             amountOfJumpsLeft = amountOfJumps;
    73.         }
    74.  
    75.         if (amountOfJumpsLeft <= 0)
    76.         {
    77.             canJump = false;
    78.         }
    79.         else
    80.         {
    81.             canJump = true;
    82.         }
    83.     }
    84.  
    85.     private void CheckMovementDirection()
    86.     {
    87.         if (isFacingRight && movementInputDirection > 0)
    88.         {
    89.             Flip();
    90.         }
    91.         else if (!isFacingRight && movementInputDirection < 0)
    92.         {
    93.             Flip();
    94.         }
    95.  
    96.         if (rb.velocity.x != 0)
    97.         {
    98.             isWalking = true;
    99.         }
    100.         else
    101.         {
    102.             isWalking = false;
    103.         }
    104.     }
    105.  
    106.     private void UptadeAnimations()
    107.     {
    108.         anim.SetBool("isWalking", isWalking);
    109.         anim.SetBool("isGrounded", isGrounded);
    110.         anim.SetFloat("yVelocity", rb.velocity.y);
    111.         anim.SetBool("isRuning", isRuning);
    112.     }
    113.  
    114.     private void CheckInput()
    115.     {
    116.         movementInputDirection = Input.GetAxisRaw("Horizontal");
    117.         if (Input.GetButtonDown("Jump"))
    118.         {
    119.             Jump();
    120.         }
    121.     }
    122.  
    123.     private void Jump()
    124.     {
    125.         if (canJump)
    126.         {
    127.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    128.         }
    129.         amountOfJumpsLeft--;
    130.  
    131.     }
    132.  
    133.     private void ApplyMovemant()
    134.     {
    135.         rb.velocity = new Vector2(movementspeed * movementInputDirection, rb.velocity.y);
    136.     }
    137.  
    138.     private void Flip()
    139.     {
    140.         isFacingRight = !isFacingRight;
    141.         transform.Rotate(0.0f, 180.0f, 0.0f);
    142.     }
    143.  
    144.     private void OnDrawGizmos()
    145.     {
    146.         Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
    147.     }
    148. }
    149.  

    Player swap code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Swicth : MonoBehaviour
    6. {
    7.     public playerMovement playerMovement;
    8.     public playerMovement playerMovement2;
    9.     public bool player1Active = true;
    10.  
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if (Input.GetKeyDown(KeyCode.LeftAlt))
    16.         {
    17.             SwitchPlayer();
    18.         }
    19.     }
    20.  
    21.     public void SwitchPlayer()
    22.     {
    23.         if (player1Active)
    24.         {
    25.             playerMovement.enabled = false;
    26.             playerMovement2.enabled = true;
    27.             player1Active = false;
    28.         }
    29.         else
    30.         {
    31.             playerMovement.enabled = true;
    32.             playerMovement2.enabled = false;
    33.             player1Active = true;
    34.         }
    35.     }
    36. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    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.
     
    Mbarocks77 likes this.
  3. Mbarocks77

    Mbarocks77

    Joined:
    Aug 14, 2020
    Posts:
    4
    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.
     
    Last edited: Sep 20, 2022