diff options
Diffstat (limited to 'Assets/Scripts/Player/PlayerMovementScript.cs')
-rwxr-xr-x | Assets/Scripts/Player/PlayerMovementScript.cs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/Assets/Scripts/Player/PlayerMovementScript.cs b/Assets/Scripts/Player/PlayerMovementScript.cs new file mode 100755 index 0000000..c28067c --- /dev/null +++ b/Assets/Scripts/Player/PlayerMovementScript.cs @@ -0,0 +1,120 @@ +using UnityEngine; +using System.Collections; + +public class PlayerMovementScript : MonoBehaviour { + + Rigidbody2D rb2d; + + //Movement + float MaxSpeed; + public float MaxNormalSpeed = 10f; + public float MaxSprintSpeed = 15f; + public float ForceFactor = 60f; + public bool FacingRight = true; + + //Movement animation + GameObject PlayerTrail; + + //Jump + public float jumpShortSpeed = 10f; + public float jumpSpeed = 30f; + bool jump = false; + bool jumpCancal = false; + public bool grounded = true; + + void Start () + { + rb2d = gameObject.GetComponent<Rigidbody2D>(); + MaxSpeed = MaxNormalSpeed; + PlayerTrail = GameObject.FindGameObjectWithTag("PlayerTrail"); + } + + void Update () + { + //Jump + if (Input.GetButtonDown("Jump") && grounded) + { + jump = true; + } + if (Input.GetButtonUp("Jump") && !grounded) + { + jumpCancal = true; + } + } + + void FixedUpdate() + { + //Horizontal + rb2d.AddForce(Vector2.right * Input.GetAxis("Horizontal") * ForceFactor); + + //Left-Right Movement + if (rb2d.velocity.x > 0.0001f) + { + transform.localScale = new Vector3(1, 1, 1); + FacingRight = true; + } + if (rb2d.velocity.x < -0.0001f) + { + transform.localScale = new Vector3(-1, 1, 1); + FacingRight = false; + } + + //Max Speed + if (rb2d.velocity.x > MaxSpeed) + { + rb2d.velocity = new Vector2(MaxSpeed, rb2d.velocity.y); + } + else if (rb2d.velocity.x < -MaxSpeed) + { + rb2d.velocity = new Vector2(-MaxSpeed, rb2d.velocity.y); + } + + //Sprint + if (Input.GetAxis("Sprint") == 1) + { + MaxSpeed = MaxSprintSpeed; + } + if (Input.GetAxis("Sprint") == 0) + { + + if (rb2d.velocity.x > MaxNormalSpeed ) + { + MaxSpeed = Mathf.Lerp(rb2d.velocity.x, MaxNormalSpeed, .1f); + } + else if (rb2d.velocity.x < -MaxNormalSpeed) + { + MaxSpeed = Mathf.Lerp(-rb2d.velocity.x, MaxNormalSpeed, .1f); + } + } + + //Sprint animation + if (rb2d.velocity.x == MaxSprintSpeed || rb2d.velocity.x == -MaxSprintSpeed) + { + PlayerTrail.SetActive(true); + } + else if (rb2d.velocity.x < 10.1f && rb2d.velocity.x > -10.1f) + PlayerTrail.SetActive(false); + + + //Jump + + if (jump) + { + rb2d.velocity = new Vector2(rb2d.velocity.x, jumpSpeed); + jump = false; + } + if (jumpCancal) + { + + if (rb2d.velocity.y > jumpShortSpeed) + rb2d.velocity = new Vector2(rb2d.velocity.x, Mathf.Lerp(rb2d.velocity.y, jumpShortSpeed, .6f)); + jumpCancal = false; + } + } + + public void StopPlayer() + { + rb2d.velocity = new Vector2(0, 0); + PlayerTrail.SetActive(false); + } +} |