- Home /
2 players. Controlling just 1.
Look, I want to make a puzzle-like game and I need to be able to play with 2 different characters. I mean, for example, 1 player must be in a pressure plate and the other in another place so you have to move each one separately. That's what I have right now, but I have no idea of how should I do it. I mean, my scripts does not control the second player or, depending on what I change, it controls both... Any help is apreciatted. Here's the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FifthSoulsMovController : MonoBehaviour {
//CAMARA
public Camera FPSCamera;
public Camera FPSCamera2;
public float horizontalSpeed;
public float verticalSpeed;
float h;
float v;
//MOVIMIENTO
public float run = 1;
public int forceConst = 4;
private bool canJump;
private Rigidbody selfRigidbody;
// Use this for initialization
void Start () {
//CAMARA
FPSCamera.enabled = true;
FPSCamera2.enabled = false;
//MOVIMIENTO
selfRigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate (){
if (canJump) {
canJump = false;
selfRigidbody.AddForce (0, forceConst, 0, ForceMode.Impulse);
}
}
// Update is called once per frame
void Update () {
//CAMARA
if (FPSCamera.enabled) {
h = horizontalSpeed * Input.GetAxis ("Mouse X");
v = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Rotate (0, h, 0);
FPSCamera.transform.Rotate (-v, 0, 0);
}
if (FPSCamera2.enabled) {
h = horizontalSpeed * Input.GetAxis ("Mouse X");
v = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Rotate (0, h, 0);
FPSCamera.transform.Rotate (-v, 0, 0);
}
if (Input.GetKeyDown (KeyCode.G)) {
FPSCamera.enabled = !FPSCamera.enabled;
FPSCamera2.enabled = !FPSCamera2.enabled;
}
//MOVIMIENTO
if (FPSCamera.enabled) {
if (Input.GetKey (KeyCode.W)) {
transform.Translate (0, 0, 0.1f * run);
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate (0.1f * run, 0, 0);
}
if (Input.GetKey (KeyCode.A)) {
transform.Translate (-0.1f * run, 0, 0);
}
if (Input.GetKey (KeyCode.S)) {
transform.Translate (0, 0, -0.1f * run);
}
if (Input.GetKey (KeyCode.LeftShift)) {
run = 2;
} else {
run = 1;
}
if (Input.GetKeyDown (KeyCode.Space)) {
canJump = true;
}
}
if (FPSCamera2.enabled) {
if (Input.GetKey (KeyCode.W)) {
transform.Translate (0, 0, 0.1f * run);
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate (0.1f * run, 0, 0);
}
if (Input.GetKey (KeyCode.A)) {
transform.Translate (-0.1f * run, 0, 0);
}
if (Input.GetKey (KeyCode.S)) {
transform.Translate (0, 0, -0.1f * run);
}
if (Input.GetKey (KeyCode.LeftShift)) {
run = 2;
} else {
run = 1;
}
if (Input.GetKeyDown (KeyCode.Space)) {
canJump = true;
}
}
}
}
Ins$$anonymous$$d of using WASD for both, why don't you use WASD for one, and the Arrows for the other one, just like on Fireboy and Watergirl?
Answer by deanpackard · Oct 13, 2017 at 06:12 PM
How do you want to be able to switch to the other target?
Answer by Stathis-Kap · Oct 14, 2017 at 11:20 AM
Your are moving the character with transform.Translate so my guess is that this script is attached to both characters. You could maybe try to create a new empty object in your Scene and attach there a script from which you can control which Character will be enabled.
For example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterController: MonoBehaviour {
public GameObject character1;
public GameObject character2;
void Start () {
character1.GetComponent<Camera>().enabled = true;
character2.GetComponent<Camera>().enabled = false;
character1.GetComponent<FifthSoulsMovController>().enabled = true;
character2.GetComponent<FifthSoulsMovController>().enabled = false;
}
void Update () {
if(Input.GetKeyDown(KeyCode.G))
{
character1.GetComponent<Camera>().enabled = !character1.GetComponent<Camera>().enabled;
character2.GetComponent<Camera>().enabled = !character2.GetComponent<Camera>().enabled;
character1.GetComponent<FifthSoulsMovController>().enabled = !character1.GetComponent<FifthSoulsMovController>().enabled;
character2.GetComponent<FifthSoulsMovController>().enabled = !character2.GetComponent<FifthSoulsMovController>().enabled;
}
}
}
And then in your FifthSoulsMovController you could let only your movement functions without checking each time which camera is enabled.
Answer by Nightm4reProds · Jan 21, 2019 at 01:31 PM
Well, I fixed it a time ago and gonna post here the solution I found.
I've just created an empty object where I've put a manager which controls what Object am I controlling with a public GameObject Target.