0
\$\begingroup\$

I have two object for the player. Whenever I press space I'd like to swap between them and also I would like to change the first player main camera to the second player camera and equally for the minimaps. It's the same as swaping a between characters in a LEGO game.
When I try this in the "Game" window, the camera should focus on the first player, but for some reason it doesn't. Neither does the first object move on WASD or on arrow keys presses. The second object is dead.
The cameras are basically a dirty trick and I really need only two. Both players have the same PlayerController script attached.

PlayerController.cs UPDATED 10.01.2018 20:45 CET - Basically what I wanted to have

using UnityEngine;

public class PlayerController : MonoBehaviour {

public float speed;

public GameObject playerAtDockCamera;
public Camera playerAtDock;

public GameObject playerAtSeaCamera;
public Camera playerAtSea;

public Rigidbody p1; //player object one
public Rigidbody p2; //player object two

private bool atSea = false;
private Rigidbody rb;
private Vector3 cameraposition;

void Start ()
{
    rb = p1;
}

private void Update()
{
    if(Input.GetKeyDown(KeyCode.Space))
    {
        atSea = !atSea;
    }
}

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");

    float moveVertical = Input.GetAxis("Vertical");

    if (!atSea)
    {
        if (rb == p2)
        {
            rb.velocity = Vector3.zero; 
            rb = p1;
        }
        EnableDock();      
        rb.AddForce(new Vector3(moveHorizontal, 0.0f, moveVertical) * speed);
       }
       else
        {
        if(rb==p1)
        {
            rb.velocity = Vector3.zero;
            rb = p2;
        }
        EnableSea();
        rb.AddForce(new Vector3(0.0f, 0.0f, moveVertical) * speed);

 }
}
void EnableSea()
{
  playerAtDock.enabled = false;
  playerAtSeaCamera.SetActive(true);
  playerAtSea.enabled = true;

}
void EnableDock()
{
    playerAtDock.enabled = true;
    playerAtSeaCamera.SetActive(false);
    playerAtSea.enabled = false;

}
}
\$\endgroup\$
  • 1
    \$\begingroup\$ You need to enable and disable components for this to work. But you haven't included the code that you use to "swap." \$\endgroup\$ – Draco18s Jan 9 '18 at 22:24
  • \$\begingroup\$ I know. I it is just Input.GetKeyDown(KeyCode.Space) to change the bool (atSea) variable. But first I would like to know why it automatically my first player object won't response. Neither my cameras are right anymore. If I uncoment the rigidbody lines nothing happens. \$\endgroup\$ – S4NNY1 Jan 9 '18 at 22:28
  • 1
    \$\begingroup\$ If you're using the same exact script for both objects, of course that won't work! Plus you aren't enabling/disabling the cameras! \$\endgroup\$ – Draco18s Jan 9 '18 at 22:34
  • \$\begingroup\$ Then I should create another object and add the script there? \$\endgroup\$ – S4NNY1 Jan 9 '18 at 22:36
1
\$\begingroup\$

You should have the following setup:

  1. One player controller script. This will always handle the input for the CURRENT game object (the one the script is attached to) and two player objects. One of these starts with its camera and player controller disaled.

  2. Code that deals with the space bar.

  3. When the space bar is pressed it gets the player controller from EACH1 player object and toggles the script's enabled property. It also enables or disables the camera associated with that player.

Trying to have the player script move both players depending on a flag in that script leads to both scripts moving the same player object (even if one is flagged on and the other flagged off) this is due to the way you've structured your if (!atSea) check: one of these will be true (moving p1) and the other will be false (moving p2 ... which is a reference to the other script's p1!)

1 If the script that handles the space bar is the player controller, then it still needs to deal with both copies: if a script disables itself, it cannot re-enable itself because it's off! I recommend having this code in an unrelated script attached elesewhere (and only a single copy).

\$\endgroup\$
  • \$\begingroup\$ I updated my script and attached it to a non-player character GameObject but still anything won't happen. I linked all of the components successfully but still no result. I don't quite understand step no 3. \$\endgroup\$ – S4NNY1 Jan 9 '18 at 23:20
  • 1
    \$\begingroup\$ You'll have to debug why its not moving on your own, your code looks correct, but there could be all sorts of things that prevent your objects from moving. If you've made your script not attached to your player objects, then your space bar script still needs to toggle the cameras and flip your boolean value in your script. \$\endgroup\$ – Draco18s Jan 10 '18 at 0:04
  • \$\begingroup\$ Hi! After some tries finally managed to get the object swapping to work. In the Start method I added rp=p1; The whole code will be updated with the current solution and tune the settinngs to make my script more responsive and fluid. Also the cameras will be reduced to two. \$\endgroup\$ – S4NNY1 Jan 10 '18 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.