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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class CharacterSwap : MonoBehaviour { public bool main; public bool secondary; public Rigidbody2D player1; public Rigidbody2D player2; void Start () { player1 = GetComponent<Rigidbody2D>(); main = true; secondary = false; player2 = GetComponent<Rigidbody2D>(); } void Update () { if(Input.GetKeyUp(KeyCode.C) && main) { player1.isKinematic = true; main = false; secondary = true; } else if(Input.GetKeyUp(KeyCode.C) && secondary) { player2.isKinematic = true; main = true; secondary = false; } } }
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?
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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public string player; public float speed; public Rigidbody2D myRigidbody; private Vector3 change; public bool player1; void Start () { myRigidbody = GetComponent<Rigidbody2D>(); } void Update() { //Movement start change = Vector3.zero; change.x = Input.GetAxisRaw("Horizontal"); change.y = Input.GetAxisRaw("Vertical"); if (change != Vector3.zero) { MoveCharacter(); } //Movement end } void MoveCharacter() //Moves player change is normalized to prevent character gaining speed while moving diagnal { myRigidbody.MovePosition( transform.position + change.normalized * speed * Time.deltaTime ); } }
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): public bool isPrimary = false; void Update() { if(isPrimary) { // Respond to User Input } else { // Logic to follow the primary Player Character } // Input code for toggling isPrimary. (outside of the isPrimary check) }