Player controller.
1,2,3 switches control mode.
1: FPS style. asdw/arrows for movement and mouse for looking.
2: Third person view. asdw/arrows for movement and looking.
3: Top down view. Same as above.
2,3: Raisable camera with mouse scroll wheel.
Switch pad with button. Walk on it to trigger the movable platform. Alternatively push the box onto it, so you can jump up on the platform. The movement script for the platform is … bad. Too be continued.
Web version of the project. (Requires Unity3D Web Player)
So I wanted to use an interface reference as a public variable, but it wouldn’t show up in the inspector. After many hours researching and trying different things, I finally settled for using abstract classes instead, and using the unity way of adding scripts to objects as my interface.
I made a pad with a button on it. This button is my switch. When something (player or box) moves onto it, it should trigger an event in another target object. So I wanted to have a SwitchTrigger interface for the target of my switch object.
class TargetObject : MonoBehaviour, ISwitchTrigger { ... void OnSwitchTrigger(bool on) { // This object has been turned on/off. } }
What I did instead was the following:
/* This should have been an interface, but Unity don't * allow interface objects as public references in the gui. * */ using UnityEngine; public abstract class SwitchTriggerTarget : MonoBehaviour { public abstract void OnTriggerSwitch(bool on); }
Then I have to create an explicit class inheriting this abstract class for the specific GameObject I want it on.
public class MovingPlatformTriggerTarget : SwitchTriggerTarget { public override void OnTriggerSwitch(bool on) { Debug.Log("Platform got trigger=" + on); GetComponent<SeekSteer>().enabled = on; } }
I then add the script SwitchTriggerTarget to my MovingPlatform object.
MovingPlatform object also has a MovingPlatform script, which basically just makes sure other objects on MovingPlatform object follows it when it moves. This is done by parenting.
public class MovingPlatform: MonoBehaviour { // TODO: Add objects on platform to an array. // Save old parent on enter, reattach old parent on exit. void OnTriggerEnter(Collider other) { other.transform.parent = gameObject.transform; } void OnTriggerExit(Collider other) { other.transform.parent = null; } }
The platform movement is done by another script. Just add the modules you want onto the GameObject, and then make a prefab of it. Modular and nice.
Now, my SwitchPad (the one you walk on to trigger the button), looks like this.
public class SwitchTrigger : MonoBehaviour { public SwitchTriggerTarget triggerObject; public AnimationClip onClip; public AnimationClip offClip; private int cnt; void Awake() { cnt = 0; if (onClip) GetComponent<Animation>().AddClip(onClip, "on"); if (offClip) GetComponent<Animation>().AddClip(offClip, "off"); } void OnTriggerEnter(Collider other) { cnt++; if (cnt == 1) { // Send trigger message to linked object. if (triggerObject) { triggerObject.OnTriggerSwitch(true); } else { Debug.Log("No trigger object attached."); } // Play on animation, if available. if (onClip) GetComponent<Animation>().Play("on"); } } void OnTriggerExit(Collider other) { cnt--; if (cnt == 0) { // Send trigger message to linked object. if (triggerObject) triggerObject.OnTriggerSwitch(false); // Play off animation, if available. if (offClip) GetComponent<Animation>().Play("off"); } } }
The highlighted row: By using the abstract class as the type, only GameObjects with scripts inheriting from SwitchTarget, like MovablePlatformSwitchTarget, can be dragged onto the target in the inspector.
This works, but everything would have been easier if an interface could be used as type and shown in inspector…
I recently found Unity and fell in love. I have been playing with it for a few days now and have started on a game. I like how modular you can make everything with the scripts you attach to the objects.
I hope anyone reading will help if I do something in a bad way, and hopefully somewhere along the ride I can help someone else with my scripts.
I have experience in C, C++, Java, Javascript, but not C#. Although, I am going for the C# route, since it seems like the better way to go.
The game will be a Mario 3D type of game. Walk around and solve puzzles. I will try to upload a web version every sunday, if there has been any progress.
Recent Comments