Some useful scripts for Unity game development (WIP)
Box Platformer Character Controller.
- Uses Rigidbody2D and BoxCollider2D.
- Uses AnimationCurve for jump time and height.
- Snap to ground while walking on slope. (no bouncing.)
- Supports one way platform.
Video:
Glorified State Machine.
Example code: Assets/Examples/GSM/NPC/NPC.cs
publicclassNPC:GSM_NormalController{privatefloattargetDist;privateinthealth=5;[HideInInspector]publicRigidbody2Drb2D;[HideInInspector]publicTransformtarget;protectedvoidAwake(){// Get other thingsgameObject.GetComponent(outrb2D);target=GameObject.Find("Target").transform;// Get statesgameObject.GetComponent(outNPC_Deathstate_death);gameObject.GetComponent(outNPC_Idlestate_idle);gameObject.GetComponent(outNPC_Attackstate_attack);gameObject.GetComponent(outNPC_Sleepstate_sleep);gameObject.GetComponent(outNPC_Followstate_follow);gameObject.GetComponent(outNPC_RunAwaystate_runAway);// Init Bvrvarbvr_Death=newBvrSingle(stateEx:null,// Extra Data for transitioning state + Additional state action.state:state_death,// state_death will be executed when this Bvr is currently active.isDone:()=>false// If this returns true, this Bvr is finished.);varbvr_follow=newBvrSingle(stateEx:null,state:state_follow,isDone:()=>targetDist<=5f);varbvr_runAway=newBvrSingle(stateEx:null,state:state_runAway,isDone:()=>targetDist>=3f);varbvr_attack=newBvrSequence(restartOnEnter:true,(stateEx:null,state:state_attack,isDone:()=>targetDist<=0.5f// If this returns true, execute state_sleep),(stateEx:null,state:state_sleep,isDone:()=>targetDist>=3f// If this returns true, this Bvr is finished.));// Init Flowvarflow_normal=newFlow();varflow_angry=newFlow();// Define Flow Logic// "flow_begin" will be always checked before checking the current flow.flow_begin.ForceDo(()=>health<=0,bvr_Death);// ForceDo(), ForceTo() is always checked even when Bvr.Wait() is not finished.flow_normal.To(()=>Input.GetKeyDown(KeyCode.Return),flow_angry)// Press Enter to change current flow to flow_angry..Do(()=>targetDist<3f,bvr_runAway).Do(()=>targetDist>5f,bvr_follow);flow_angry.Do(()=>true,bvr_attack.Wait()// bvr_attack.Wait() -> Don't check Do(), To() until bvr_attack is finished..To(()=>true,flow_normal));// bvr_attack.To() -> Change current flow to flow_normal if bvr_attack is finished.// Init GSMGSM_Init(startingFlow:flow_normal,defaultStateEx:null,defaultState:state_idle);}protectedoverridevoidUpdate(){targetDist=Vector2.Distance(target.position,transform.position);base.Update();// This is necessary! (processing flow, bvr, etc... is done here.)}privatevoidOnMouseDown(){health--;// Click 5 times to kill this NPC}}- Helper extension methods
- Singleton
- Timer
- Add Object pooling
- Add UI Components
- Add more useful scripts
- Fix bugs
- Use OpenUPM format
