Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- //Classes so we don't need a few hundred scripts
- internal class input
- {
- //Values acessed when declared
- public float Tilth;
- //Debounces
- public bool flipping;
- public bool jumping;
- public bool jumped;
- public bool jchk;
- //Get our inputs and overwrite values to persist those inputs for the frame
- public void getInput()
- {
- if (Input.GetKey(KeyCode.D)) //Simple if, elseif, else conditional. GetKey also works for continuous input
- {
- Tilth = Mathf.Lerp(Tilth, 1, Time.smoothDeltaTime * 10); //A lerp for keyboard users, to make it feel smooth
- }
- else if (Input.GetKey(KeyCode.A))
- {
- Tilth = Mathf.Lerp(Tilth, -1, Time.smoothDeltaTime * 10); // ^
- }
- else { Tilth = Mathf.Lerp(Tilth, 0, Time.smoothDeltaTime * 10); } //^^
- if (Input.GetKey(KeyCode.Space))
- {
- if (!jumping) { jchk = true; jumping = true; }
- }
- }
- //Update the player based on those inputs
- public void updateP(GameObject p,float speed,float jpow)
- {
- //If it's not 0 and currently not completely flipped then
- if (Mathf.Round(Tilth) != 0 && (Tilth < 0 && p.GetComponent<SpriteRenderer>().flipX != true) || (Tilth > 0 && p.GetComponent<SpriteRenderer>().flipX != false))
- {
- flipping = true; //Start flipping, this is a debounce
- p.transform.localScale = Vector3.Lerp(p.transform.localScale, new Vector3(0.02f, .75f, 1), Time.deltaTime * 10); //ANIMOOT [in]
- }
- else { flipping = false; p.transform.localScale = Vector3.Lerp(p.transform.localScale, new Vector3(.75f, .75f, 1), Time.deltaTime * 10); } //If not flipping then return to normal
- //If approximatly done flipping but flipping then actually flip
- if (p.transform.localScale.x < 0.075 && Tilth < 0 && flipping) { p.GetComponent<SpriteRenderer>().flipX = true;}
- if (p.transform.localScale.x < 0.075 && Tilth > 0 && flipping) { p.GetComponent<SpriteRenderer>().flipX = false;}
- //Same conditions as above, but now we animate [out]
- if (p.transform.localScale.x < 0.075 && flipping ) {
- p.transform.localScale = Vector3.Lerp(p.transform.localScale, new Vector3(.75f, .75f, 1),Time.deltaTime * 10);
- }
- //If approximatly done and flipping then stop flipping
- if ((Mathf.Approximately(.75f, p.transform.localScale.x) || p.transform.localScale.x > .73f) && flipping)
- {
- flipping = false;
- }
- if (jumping && !jumped && !jchk)
- {
- p.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jpow));
- jumped = true;
- }
- // Set velocity to speed*tilt smoothly
- p.GetComponent<Rigidbody2D>().velocity = Vector2.Lerp(p.GetComponent<Rigidbody2D>().velocity,new Vector2(speed*Tilth, p.GetComponent<Rigidbody2D>().velocity.y),Time.smoothDeltaTime*15);
- }
- }
- internal class camr
- {
- //Allow you to get and set the camera
- public Camera cam { get; set; }
- //Last camera position, for the camera follow
- private Vector3 lCamP; //Which stops at around the end of the screen, and follows smoothly
- //Function for updating the camera position
- public void updateCam(Vector3 newPos)
- {
- cam.transform.position = Vector3.Slerp(lCamP, newPos, Time.smoothDeltaTime*7.5f); //Slerp feels a bit better than Lerp
- lCamP = cam.transform.position; //Overwrite the old position for the last camera position
- }
- //Initialization function that I never used
- public camr()
- {
- }
- }
- internal class physics
- {
- public bool grounded(Vector3 position)
- {
- if (Physics2D.Raycast(position + new Vector3(0, -1.5f, 0), Vector2.down, .0025f)) //Raycast to maybe see the ground
- {
- return true;
- }
- else return false;
- }
- }
- //Required stuffs
- public class playerFramework : MonoBehaviour {
- //Value declaration
- public GameObject plr;
- public Camera camObj;
- public Vector3 camOffset;
- public float movespeed_default;
- public float jumppow_default;
- //Non-configurable values
- private camr cam; //Create our camera controller
- private input controller; //Create our input controller
- private physics phys; //Create our physics controller (Prolly could just be functions but meh)
- // Initialization function
- void Start ()
- {
- //Loading the classes
- cam = new camr();
- controller = new input();
- phys = new physics();
- //Setting the camera
- cam.cam = camObj;
- }
- void Update ()
- {
- //Call the updates for things like camera position and getting input
- cam.updateCam(plr.transform.position + camOffset);
- controller.getInput();
- controller.updateP(plr,movespeed_default,jumppow_default);
- }
- void LateUpdate()
- {
- if (controller.jchk)
- {
- if (phys.grounded(plr.transform.position))
- {
- controller.jchk = false;
- }
- else { controller.jumping = false; controller.jumped = false; controller.jchk = false; }
- }
- if (controller.jumped && !controller.jchk)
- {
- if (phys.grounded(plr.transform.position))
- {
- controller.jumping = false;
- controller.jumped = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement