Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Alan 3 · Mar 29, 2011 at 08:17 PM · javascriptmodelswitchswitch characters

Changing player model with keypress.

Hello,

I am having trouble coming up with the script for when the player presses a key, the current character model will switch to another model.

I am using javascript.

if (Input.GetButton ("1")) {
 //player switches character
}

I have the 3 models in the hierarchy but I am not sure how I would call on them to switch when the key is pressed.

Thanks for any help and time taken.

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

3 Replies

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

Answer by demize2010 · Mar 29, 2011 at 08:24 PM

I have a feeling that this may get a bit more complicated then you currently suspect if these are animated or moving objects etc... but for what your asking you'll need to destroy/disable the current model and and instantiate/enable the next one. The simplest option being:

currentModel.SetActiveRecursively(false);

newModel.SetActiveRecursively(true);

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 Alan 3 · Mar 29, 2011 at 08:30 PM 0
Share

Thanks for help there.

Yeah I only just started and wasn't really to sure on the difficulty. I intend to have all the models moving. I thought I would just have to attach the movement scripts onto the model but I'll take a further look into it.

Thanks

avatar image demize2010 · Mar 29, 2011 at 08:46 PM 0
Share

The way to do it would to have a base gameobject with your movement script and/or rigidbody on it. Then make the models children of that object ;)

Animations are going to get tougher but not too much, if their animations have the same name you just need a current$$anonymous$$odel variable.

If you want to seamlessly blend from one models position to another - eg match up the bone positions - it may get a bit tougher.

avatar image Alan 3 · Mar 29, 2011 at 09:42 PM 0
Share

Hi I have just done that. Are the models expected to follow the gameObect when parented? I have created a empty gameObject that has two of the models parented to it. Only the gameObject moves along with the $$anonymous$$ainCamera.

avatar image Alan 3 · Mar 29, 2011 at 10:34 PM 0
Share

Thanks for the help! I've got it working now.

Cheers!

avatar image
1
Best Answer

Answer by Tommy · Mar 29, 2011 at 08:27 PM

Here's a little script the might work:

var modelOne = GameObject.Find("Player1"); var modelTwo = GameObject.Find("Player2"); var modelThree = GameObject.Find("Player3");

if (Input.GetKeyDown ("1")) { modelOne.SetActiveRecursively(true); modelTwo.SetActiveRecursively(false); modelThree.SetActiveRecursively(false); } else if (Input.GetKeyDown ("2")) { modelOne.SetActiveRecursively(false); modelTwo.SetActiveRecursively(true); modelThree.SetActiveRecursively(false); } else if (Input.GetKeyDown ("3")) { modelOne.SetActiveRecursively(false); modelTwo.SetActiveRecursively(false); modelThree.SetActiveRecursively(true); }

Hope this will help you out!

Comment
Add comment · Show 5 · 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 Alan 3 · Mar 29, 2011 at 08:57 PM 0
Share

Hi, thanks for the reply.

Should I put this as a script on it's own? I've tried adding it along with my movement keys but keep getting Unity Exception Input Button 1 is not a setup.

avatar image Tommy · Mar 29, 2011 at 09:01 PM 0
Share

You could make this a script itself, make sure you add the gameobjects to the vars.

avatar image Tommy · Mar 29, 2011 at 09:14 PM 0
Share

Strange, i get the same error... I'll look into it! Edited the code btw.

avatar image Alan 3 · Mar 29, 2011 at 09:50 PM 0
Share

I have added the scripts now, following demize2010's player organizing, the models are now in a gameObject. The script is attached along with the models dragged onto the scripts but nothing is changing as well as all models are displayed on screen at the same time.

avatar image Alan 3 · Mar 29, 2011 at 10:33 PM 0
Share

Thanks for the time and help, I've got it working now!

Cheers!

avatar image
0

Answer by simjam03 · Aug 22, 2011 at 03:56 PM

Hi Alan just wondering if you could link me to demize2010 player organizing if possible.

cheers

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

1 Person is following this question.

avatar image

Related Questions

Changing variable numbers depending on current player model. 1 Answer

Switch Statement question 0 Answers

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

Player morphing to different model 0 Answers

Switch Statements 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