Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nightm4reProds · Oct 13, 2017 at 03:08 PM · camerafpscontrollercontrol2 player

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;
             }
         }    
 
     }
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Igor_Vasiak · Oct 13, 2017 at 04:11 PM 0
Share

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?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by deanpackard · Oct 13, 2017 at 06:12 PM

How do you want to be able to switch to the other target?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

How do I attach a character to a fps controller / make him rotate? 0 Answers

Keyboard / Mouse control for FPS 0 Answers

How to manually install plugin into Unity? 2 Answers

How to add controller input to keyboard input 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges