Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour {
- public float speed = 2;
- public float jumpSpeed = 2;
- public float maxSpeed = 5;
- public float dragOutput;
- public float constantDragOutput;
- public AnimationCurve dragCurve;
- public float dragBelowInput = .1f;
- public AnimationCurve constantDragCurve;
- public Transform weaponSlot;
- public Transform leftArm;
- public Transform rightArm;
- public Transform head;
- public Transform graphics;
- public Transform groundRaycaster;
- public Transform feetLocationTransform;
- public float raycastDistance = .1f;
- float moveInput;
- float jumpInput;
- Vector2 _velocity;
- Animator AnimatorComponent { get; set; }
- public bool IsGrounded { get; private set; }
- Rigidbody2D Rigidbody2DComponent { get; set; }
- public Vector2 Velocity {
- get { return _velocity; }
- private set { _velocity = value; }
- }
- Dictionary<Collider2D, int> colliderLayers = new Dictionary<Collider2D, int>();
- void OnEnable() {
- AnimatorComponent = GetComponent<Animator>();
- Rigidbody2DComponent = GetComponent<Rigidbody2D>();
- //foreach(Collider2D collider in GetComponentsInChildren<Collider2D>())
- // colliderLayers[collider] = collider.gameObject.layer;
- }
- void Update() {
- dragOutput = dragCurve.Evaluate(Mathf.Abs(Rigidbody2DComponent.velocity.x));
- constantDragOutput = constantDragCurve.Evaluate(Mathf.Abs(Rigidbody2DComponent.velocity.x));
- AnimatorComponent.SetFloat("Speed", Mathf.Abs(Rigidbody2DComponent.velocity.x));
- DetectDrag();
- DetectGround();
- DetectJump();
- }
- void LateUpdate() {
- Vector2 direction = (weaponSlot.position - leftArm.position).normalized;
- leftArm.eulerAngles = new Vector3(0, 0, Mathf.Atan2(-direction.y, -direction.x) * Mathf.Rad2Deg + 180);
- direction = (weaponSlot.position - rightArm.position).normalized;
- rightArm.eulerAngles = new Vector3(0, 0, Mathf.Atan2(-direction.y, -direction.x) * Mathf.Rad2Deg + 180);
- direction = (weaponSlot.position - head.position).normalized;
- float horizontalScale = Mathf.Sign(graphics.localScale.x);
- head.eulerAngles = new Vector3(0, 0, Mathf.Clamp(Mathf.Atan2(-direction.x * horizontalScale, direction.y) * Mathf.Rad2Deg + 90, -80, 60));
- }
- void FixedUpdate() {
- Vector3 velocity = Rigidbody2DComponent.velocity;
- if(IsGrounded)
- if(Mathf.Abs(moveInput) > .1f) {
- if(Mathf.Sign(moveInput) > 0) {
- if(velocity.x < 0) velocity.x = 0;
- }
- else {
- if(velocity.x > 0) velocity.x = 0;
- }
- Rigidbody2DComponent.velocity = velocity;
- }
- //Debug.Log((Mathf.InverseLerp(-maxSpeed * 2, maxSpeed * 2, Rigidbody2DComponent.velocity.x) - .5f) * 2);
- Rigidbody2DComponent.AddForce(Vector2.right * moveInput * speed);
- }
- void OnDrawGizmos() {
- Gizmos.color = Color.green;
- Gizmos.DrawRay(groundRaycaster.position, Vector3.down * raycastDistance);
- }
- void DetectDrag() {
- Vector2 velocity = Rigidbody2DComponent.velocity;
- if(IsGrounded && Mathf.Abs(moveInput) < dragBelowInput)
- velocity.x -= velocity.x * Time.deltaTime * dragCurve.Evaluate(Mathf.Abs(velocity.x));
- velocity.x -= velocity.x * Time.deltaTime * constantDragCurve.Evaluate(Mathf.Abs(velocity.x));
- Rigidbody2DComponent.velocity = velocity;
- }
- void DetectGround() {
- //foreach(KeyValuePair<Collider2D, int> colliderLayer in colliderLayers)
- // colliderLayer.Key.gameObject.layer = Physics2D.IgnoreRaycastLayer;
- RaycastHit2D hit = Physics2D.Raycast(groundRaycaster.position, -Vector2.up, raycastDistance);
- if(hit.collider) {
- if(Rigidbody2DComponent.velocity.y <= 0) {
- Rigidbody2DComponent.gravityScale = 0;
- Vector3 position = transform.position;
- position.y = position.y - (feetLocationTransform.position.y - hit.point.y);
- transform.position = position;
- }
- IsGrounded = true;
- }
- else {
- Rigidbody2DComponent.gravityScale = 1;
- IsGrounded = false;
- }
- //foreach(KeyValuePair<Collider2D, int> colliderLayer in colliderLayers)
- // colliderLayer.Key.gameObject.layer = colliderLayer.Value;
- }
- void DetectJump() {
- if(jumpInput > .5f) {
- if(IsGrounded && Rigidbody2DComponent.velocity.y <= 0) {
- Vector3 velocity = Rigidbody2DComponent.velocity;
- velocity.y = jumpSpeed;
- Rigidbody2DComponent.velocity = velocity;
- }
- }
- }
- public void Attack() {
- IWeapon weapon = weaponSlot.GetComponentInChildren<IWeapon>();
- if(weapon != null) weapon.Attack();
- }
- public void MoveInput(float speed) {
- moveInput = Mathf.Clamp(speed, -1, 1);
- }
- public void JumpInput(float jump) {
- jumpInput = jump;
- }
- public void SetWeaponAngle(float angle) {
- bool up = Mathf.Abs(180 - (angle)) >= 90;
- graphics.localScale = new Vector3(up ? 1 : -1, graphics.localScale.y);
- leftArm.localScale = new Vector3(leftArm.localScale.x, up ? 1 : -1);
- rightArm.localScale = new Vector3(rightArm.localScale.x, up ? 1 : -1);
- leftArm.GetComponentInChildren<SpriteRenderer>().sortingOrder = up ? 2 : -2;
- rightArm.GetComponentInChildren<SpriteRenderer>().sortingOrder = up ? -2 : 2;
- weaponSlot.parent.localEulerAngles = new Vector3(0, 0, angle);
- }
- public void SetWeaponAngle(Vector2 direction) {
- float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 180;
- SetWeaponAngle(angle);
- }
- public void SetWeapon(Transform weapon) {
- foreach(Transform currentWeapon in weaponSlot) {
- currentWeapon.SetParent(null);
- Rigidbody2D rigidbody2D = currentWeapon.GetComponent<Rigidbody2D>();
- rigidbody2D.isKinematic = false;
- rigidbody2D.velocity = Rigidbody2DComponent.velocity;
- foreach(Collider2D collider in currentWeapon.GetComponentsInChildren<Collider2D>(true))
- collider.enabled = true;
- }
- if(weapon) {
- weapon.GetComponent<Rigidbody2D>().isKinematic = true;
- weapon.SetParent(weaponSlot, false);
- weapon.localPosition = Vector3.zero;
- weapon.localRotation = Quaternion.identity;
- foreach(Collider2D collider in weapon.GetComponentsInChildren<Collider2D>(true))
- collider.enabled = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment