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 /
  • Help Room /
avatar image
0
Question by dirtbikeguy55 · Jul 16, 2016 at 04:26 AM · c#movementcharactercharacter controller

Controller script active on both characters.

Hi, I have a controller script that is on two characters and when one character is active and I try to control him they both move. So I was just wondering if someone knows a way to fix this. Everything else works fine like that camera switching. If someone could help me that would be great thanks. These scripts are on a prefab that will be spawned in at the start of the scene.

 public class MyFPSController : MonoBehaviour {
 
     public float Speed;
     public unitManager myUnitManagerScript;
 
     public GameObject UnitBeingControlled;
 
     void Start()
     {
         UnitBeingControlled = this.gameObject;
     }
 
     void Update()
     {
         if (myUnitManagerScript.GetComponent<unitManager>().InFP == true)
         {
 
             //Controls the unit movement
             if (Input.GetKey(KeyCode.W))
             {
                 UnitBeingControlled.transform.Translate(Vector3.forward * Time.deltaTime * Speed);
             }
 
 
             if (Input.GetKey(KeyCode.A))
             {
                 UnitBeingControlled.transform.Translate(Vector3.left * Time.deltaTime * Speed);
             }
 
             if (Input.GetKey(KeyCode.S))
             {
                 UnitBeingControlled.transform.Translate(Vector3.back * Time.deltaTime * Speed);
             }
 
             if (Input.GetKey(KeyCode.D))
             {
                 UnitBeingControlled.transform.Translate(Vector3.right * Time.deltaTime * Speed);
             }
         }
     }
 }

 public class unitManager : MonoBehaviour {
 
     public bool hover = false;
     public bool selected = false;
     public bool NameMatches = false;
     public bool InFP = false;
     public bool FPSCameraEnabled = false;
 
     public GameObject FPSCamera;
     public GameObject MainCamera;
 
     public GameObject Unit;
 
     void Start () {
 
         Unit = this.gameObject;
 
         FPSCamera = this.gameObject.transform.GetChild(0).gameObject;
 
         InFP = false;
 
     }
 
     void Update()
     {
         //Enters First Person
         if (Input.GetKeyDown(KeyCode.Return))
         {
             InFP = true;
         }
 
 
         //Activates First Person Cmaera
         if (hover == true && selected == true && InFP == true)
         {
             FPSCamera.active = true;
             MainCamera.active = false;
         }
 
         //Leaves First Person
         if(Input.GetKeyDown(KeyCode.Escape) && InFP == true)
         {
             MainCamera.active = true;
             FPSCamera.active = false;
             InFP = false;
             //FPSCameraEnabled = false;
         }

     }
 
     void OnMouseOver()
     {
         hover = true;
     }
 
     void OnMouseExit()
     {
         hover = false;
         selected = false;
     }
 
     void OnMouseDown()
     {
         selected = true;
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by LuckierBread · Jul 16, 2016 at 04:45 AM

I'm no pro but it looks like the problem is that both characters are the same prefab.

If that is the case then they are both identifying themselves as UnitBeingControlled because they both run the following on startup.

  void Start()
  {
      UnitBeingControlled = this.gameObject;
  }

You might need to have two prefabs with slightly different scripts identifying them by tag or something.

Comment
Add comment · Show 4 · 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 dirtbikeguy55 · Jul 16, 2016 at 04:52 AM 0
Share

Well that's the thing is that the script is identifying them properly as different characters, but it just dosent want to work.

alt text

character.png (75.8 kB)
avatar image dirtbikeguy55 · Jul 16, 2016 at 04:55 AM 0
Share

Ok just tested what you said and it might work ill let you know. I didn't even think of that thanks.

avatar image dirtbikeguy55 · Jul 16, 2016 at 04:58 AM 0
Share

Ok so when I add the unit$$anonymous$$anager script to the fpscontroller script in the inspector, it controls both of the characters(not prefabs) but if I don't define anything only one character moves.

avatar image dirtbikeguy55 · Jul 16, 2016 at 07:11 AM 0
Share

Ok I figured it out, I had to add a few true false statements lol so easy, but I guess I just overthought it. Thanks for the help!

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

200 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 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

MoveTowards is curving for no reason 0 Answers

Raycast Check in GetKey? 0 Answers

How to move whilst airborne (using standard third person character script)? 0 Answers

how to move only one object ? 1 Answer

restricting player movement with a path? 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