Search Unity

Character swapping help

Discussion in 'Scripting' started by HoppingFish, Jan 15, 2019.

  1. HoppingFish

    HoppingFish

    Joined:
    Jan 29, 2018
    Posts:
    6
    I am trying to make a 2d rpg with 2 characters that you can swap between I want the one that you aren't playing to follow the one you are playing as so I don't just want to disable the original character when swapping as I want the main character to follow the secondary character around after I switch and vice-versa. Originally I was trying to do player1.enabled = false; but as rigidbody2d doesn't have a enable and disable this didn't work, wondering if there is a work around for this without having to switch to character controllers



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterSwap : MonoBehaviour {
    6.     public bool main;
    7.     public bool secondary;
    8.     public Rigidbody2D player1;
    9.     public Rigidbody2D player2;
    10.    
    11.     void Start () {
    12.         player1 = GetComponent<Rigidbody2D>();
    13.         main = true;
    14.         secondary = false;
    15.         player2 = GetComponent<Rigidbody2D>();
    16.        
    17.     }
    18.    
    19.    
    20.     void Update () {
    21.        
    22.         if(Input.GetKeyUp(KeyCode.C) && main)
    23.         {
    24.             player1.isKinematic = true;
    25.             main = false;
    26.             secondary = true;
    27.         }
    28.         else if(Input.GetKeyUp(KeyCode.C) && secondary)
    29.         {
    30.             player2.isKinematic = true;
    31.             main = true;
    32.             secondary = false;
    33.         }
    34.     }
    35. }
    36.  
     
  2. HoppingFish

    HoppingFish

    Joined:
    Jan 29, 2018
    Posts:
    6
  3. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Setting isKinematic is how you enable/disable rigidbodies, but you could also use
    rigidbody.detectCollisions = false
    . You might be having a problem because that code doesn't appear to set isKinematic to false at any point. Maybe you want to do this
    rigidbody1.isKinematic = true; rigidbody2.isKinematic = false;
    when toggling?
     
  4. HoppingFish

    HoppingFish

    Joined:
    Jan 29, 2018
    Posts:
    6
    I appreciate the help I have edited the code and it is now working properly the issue I'm still having is both players move with the movement script I have set even though they are set as isKinematic.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class Player : MonoBehaviour {
    8.  
    9.     public string player;
    10.     public float speed;
    11.     public Rigidbody2D myRigidbody;
    12.     private Vector3 change;
    13.     public bool player1;
    14.  
    15.  
    16.    
    17.     void Start () {
    18.         myRigidbody = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.    
    22.     void Update()
    23.     {
    24.    
    25.         //Movement start
    26.         change = Vector3.zero;
    27.         change.x = Input.GetAxisRaw("Horizontal");
    28.         change.y = Input.GetAxisRaw("Vertical");
    29.         if (change != Vector3.zero)
    30.           {
    31.                 MoveCharacter();
    32.           }
    33.         //Movement end
    34.        
    35.     }
    36.     void MoveCharacter() //Moves player change is normalized to prevent character gaining speed while moving diagnal
    37.     {
    38.         myRigidbody.MovePosition(
    39.             transform.position + change.normalized * speed * Time.deltaTime
    40.             );
    41.     }
    42. }
    43.  
     
  5. HoppingFish

    HoppingFish

    Joined:
    Jan 29, 2018
    Posts:
    6
    I resolved the issue by just enabling and disabling the script, I appreciate all your help Wallace.
     
  6. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    If that script is attached to both characters, then you'll need to disable the movement for one of them, maybe with a toggle?
    Code (csharp):
    1. public bool isPrimary = false;
    2. void Update()
    3. {
    4.    if(isPrimary) {
    5.       // Respond to User Input
    6.    } else {
    7.       // Logic to follow the primary Player Character
    8.    }
    9.    // Input code for toggling isPrimary. (outside of the isPrimary check)
    10. }