I have a few questions with Unity: one that came up recently and a couple that have been lingering the whole time I’ve been using Unity.
1. What is the best way to switch between/control multiple playable characters? I have tried enabling/disbabling scripts because of the lack of scripting ability to change arguments to other scripts.
2. Is there a way to change arguments to scripts? I just realized that maybe the solution to this is to broadcast/send messages telling the other scripts to activate/deactivate. Is this a good idea or is there another solution?
3. Is there a way to active/deactive scripts by name (not just index), add scripts, and remove scripts? I seem to remember something about creating prototype objects and that might have something to do with setting up object scripts, but I’m not sure.
Good question
Im loading different levels by collisions, but ideally getting in and out of a car is what Im looking for.
The thing with my example is when the player "chooses(havent got there with this yet)“to get in the car, if the fps Prefab is"disabled” it would then need to be parented to the car so when the player gets back out of the car at a different location, they’re not back at the origional location, but are respawned at the cars new location.
Sorry I cant answer your question, I hope someone does
AC
It depends. If the different characters behave in mostly the same way, you could have an empty object with a charactercontroller and swap the parents of the various character models. Do all your code with the empty charactercollider object.
What do you mean “change arguments to other scripts”? Are you referring to, for example, collisions with enemy bullets? There are a few ways to do this; I would recommend an empty gameobject with a script; put the various characters into script variables; and call GameObject.Find(“DummyObjectName”).GetComponent(“CharSwitcher”).getCurrentCharacterObject(); (or make it a static function) have that function return the character object that is currently active.
Unfortunately, the different characters have vastly different physics, sizes, script behaviors, etc.
I meant change the variables in one script from within another script. I think you’ve put me on the right track though; I somehow only figured out how to get children of a certain type. I guess I didn’t catch the second example in the documentation for GetComponent() (http://unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html) that shows how you can use a script name. Also, I didn’t realize you can run script functions using this. It looks like now I can have setters/getters to activate/deactivate an object’s control script as the active player, while still allowing parts of the script to run to control drag or otherwise. Thanks!
Yep, this makes sense now. Before I think I was trying:
and then looping through the resulting array and setting “.enabled”.
I’m still milling about the best way to do character switching with very different character (for example the person character switching with a car character).
One solution I thought of is have an empty object with a script that takes in all of the player’s inputs and deals with them. This object is paired with a model in the scene and tells that model what to do. (Similar objects could do the same work for remote networked players).
However, I’m not sure which way to go with attachment of the player object to the character. The player object could just sit (0,0,0) and apply forces to the player or tell its script what to do. The player object could be the child of the character, allowing the camera to follow the player, not the character around, but I don’t know how that would work with prefabs. The player could be the parent of the character, but that may cause problems with physics. I like the parent or child idea because the camera could zoom from one place to another by just following the player, but some advice on the abilities of prefabs, physics, and changing relations with scripts would be helpful.
If anyone has any completely different, better ideas, let me know, too. I wonder if this is the right way to go to work with that serverless network script that’s been featured on the main page. Thanks.
I think in the case of vehicle and character the best way is to just have completely seperate game object hierarchies and use activate/deactivate.
First of all the Car should have a property named, isUserControlled. Only if isUserControlled is enabled, will the car actually drive. You probably don’t want to override .enabled for this, because in some vehicles you still want scripts executing even if they are not being driven. Eg. a boat still needs to apply forces to keep itself above the water. When a character enters a car isUserControlled is enabled, when he exits it is disabled.
Now what do you do with the character while he is in the car.
The simple answer is: Nothing
If he is invisible you deactivate the entire character game object hierarchy. The car simply keeps a reference to the root game object of the character, and when the player wants to exit the car, you simply activate the entire character hierarchy again. (SetActiveRecursively is helpful here)
Now you might want to have the character visible while he is in the car. But don’t let this get you off track and come up with an overly complex solution.
The setup for a character driving in a car is completely different than a normal walking charater. Thus keep them seperate.
So you still want to deactivate the entire character, but now you just instantiate a prefab that is presetup just for driving in a car. I am thinking of some kind of ragdoll setup with a couple of hinge joints attaching it to wheels and seat. When the player exits the car, again you delete this player ragdoll completely and activate the old player hieararchy again.
Theres a beginners option there, by using active=true/false, and a third party trigger that does the switching. If you understand Joachims angle go with that.
AC