I'm using C#
I'm making a game there you can jump into objects and controll them. And I use booleans to make the game to switch controlls between the player and objects (if soulcontroll = true, you can controll the player). I made it before with another objects, and it works perfectly! but now when I'm trying to fix it to another objects, it won't turn soul controll to false =S
here is the code for the trigger that decides if you can jump into the object:
and here is the code for the object I will controll:
asked Nov 18 '11 at 09:19 AM
The way I would do this would be to have all of the objects which can be inhabited share a common interface, so that you can refer to lots of different kinds of behaviour all in the same slot!
As you know, when you declare a 'Vector3' in your script, it is equivalent to making a Vector3-shaped imprint in your memory, that the program knows how to manipulate. What actually goes in there has to be a Vector3, but can be any Vector3 you like. The program knows that it can use methods like 'Scale', 'Normalize', 'sqrMagnitude' and so on, because you have told it exactly what kind of object it will be.
This is where interfaces come in.
While a class can only inherit directly from one parent class, it can also agree to conform to any number of 'interfaces' - simply, the class says that while it may have more than just these, it will definitely have some set of functions which can be called in a way that conforms to the interface.
This allows you to make a 'hole' shaped like the interface, and have lots of different classes (as long as they conform to the interface) fit into it!
You declare an interface like this-
Each line inside the interface defines a method that each class that conforms to it must implement in some way- in this case, anything that implements 'Inhabitable' must have at least-
You can do whatever you like inside these methods- as long as the return type and the parameters are the same it's all fair game.
To use one of these interfaces in a Unity class, it must be declared after the class name-
To use the interface for what you are doing here, you would have as one of the member variables of your player script
This defines a reference which can refer to any class that inherits from Inhabitable.
In your 'Start' script, you would set it up so that the 'default' body (is it some kind of ghost? I'll assume it is) gets set as the currentlyInhabiting variable.
This is, of course, assuming you have a class called Ghost on the same object as the 'Player'- you might not, I'm not sure. In any case, it'd look (minimally) something like this-
Then, in your 'Player' update method, call the currently inhabited object's UpdateInhabited-
Set up a simple method for changing inhabited objects, like this-
When you want to change objects (say to a haunted box that's lying around) (you're the one haunting it), if you know that a given object will have a certain component on it, and you know that that component is also Inhabitable, you can use this-
And that will automatically change what the player is inhabiting, through the magic of interfaces.
answered Nov 18 '11 at 11:08 AM