diff options
Diffstat (limited to 'Assets/Scripts')
-rw-r--r-- | Assets/Scripts/CharacterController2D.cs | 154 | ||||
-rw-r--r-- | Assets/Scripts/CharacterController2D.cs.meta | 11 | ||||
-rw-r--r-- | Assets/Scripts/FollowCamera.cs | 32 | ||||
-rw-r--r-- | Assets/Scripts/FollowCamera.cs.meta | 11 | ||||
-rw-r--r-- | Assets/Scripts/InfiniteBackground.cs | 41 | ||||
-rw-r--r-- | Assets/Scripts/InfiniteBackground.cs.meta | 11 |
6 files changed, 260 insertions, 0 deletions
diff --git a/Assets/Scripts/CharacterController2D.cs b/Assets/Scripts/CharacterController2D.cs new file mode 100644 index 0000000..bf62498 --- /dev/null +++ b/Assets/Scripts/CharacterController2D.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections; +using UnityEngine; +using UnityEngine.Events; + +[RequireComponent(typeof(Rigidbody2D))] +[RequireComponent(typeof(CapsuleCollider2D))] + +public class CharacterController2D : MonoBehaviour +{ + public enum Direction + { + left = -1, + right = 1 + } + + [SerializeField] Camera mainCamera; + [SerializeField] float acceleration = 40f; + [SerializeField][Range(0, .3f)] float smoothing = .05f; + [SerializeField] float gravityScale = 1.5f; + [SerializeField] float jumpForce = 50f; + [SerializeField] int maxJumpCount = 1; + [SerializeField] bool airControl = true; + + internal UnityEvent OnIdleEvent; + internal UnityEvent OnFlip; + internal UnityEvent OnJump; + internal UnityEvent OnLand; + internal UnityEvent OnFall; + + public Direction direction = Direction.right; + public Vector2 velocity = Vector2.zero; + public int jumpCount = 0; + public bool isGrounded = false; + public Vector3 cameraPos; + public Rigidbody2D _rigidBody; + public CapsuleCollider2D _collider; + public Transform _transform; + + void Awake() + { + _rigidBody = GetComponent<Rigidbody2D>(); + _collider = GetComponent<CapsuleCollider2D>(); + OnIdleEvent ??= new UnityEvent(); + OnFlip ??= new UnityEvent(); + OnJump ??= new UnityEvent(); + OnFall ??= new UnityEvent(); + OnLand ??= new UnityEvent(); + } + + // Use this for initialization + void Start() + { + _transform = transform; + _rigidBody.freezeRotation = true; + _rigidBody.collisionDetectionMode = CollisionDetectionMode2D.Continuous; + _rigidBody.gravityScale = gravityScale; + direction = (Direction)(_transform.localScale.x / Mathf.Abs(_transform.localScale.x)); + + jumpCount = maxJumpCount; + + if (mainCamera) + cameraPos = mainCamera.transform.position; + + OnFlip.AddListener(HandleFlip); + OnJump.AddListener(HandleJump); + OnFall.AddListener(HandleFall); + OnLand.AddListener(HandleLand); + } + + void Update() + { + if (isGrounded || airControl) + HandleHorzInput(Input.GetAxisRaw("Horizontal")); + + // Changing this to `canJump` or `jumpCount > 0` would allow for things like double jumping. + if (jumpCount > 0 && Input.GetButtonDown("Jump")) + OnJump.Invoke(); + + + if (mainCamera) + { + var targetPos = new Vector3(_transform.position.x, cameraPos.y, cameraPos.z); + mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, targetPos, 0.25f); + } + } + + void FixedUpdate() + { + CheckCollision(); + } + + public void Move(float magnitude) + { + Vector3 targetVelocity = new Vector2(magnitude, _rigidBody.velocity.y); + _rigidBody.velocity = Vector2.SmoothDamp(_rigidBody.velocity, targetVelocity, ref velocity, smoothing); + } + + void CheckCollision() + { + var evnt = isGrounded ? OnFall : OnLand; + + float colliderRadius = _collider.size.x * 0.4f * Mathf.Abs(transform.localScale.x); + Vector3 groundCheckPos = new Vector3(_collider.bounds.size.x * 0.5f, colliderRadius * 0.9f, 0) + _collider.bounds.min; + Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckPos, colliderRadius); + + if (Array.Exists(colliders, c => c != _collider)) + { + Debug.Log(Array.Find(colliders, c => c != _collider)); + evnt.Invoke(); + } + } + + void HandleHorzInput(float horzInput) + { + Move(horzInput * acceleration); + + if (horzInput * (int)direction < 0) + OnFlip.Invoke(); + } + + + void HandleFlip() + { + Debug.Log("Flipping"); + Vector3 theScale = transform.localScale; + theScale.x *= -1; + transform.localScale = theScale; + + direction = direction == Direction.left ? Direction.right : Direction.left; + } + + void HandleJump() + { + Debug.Log("Jumping"); + jumpCount--; + _rigidBody.AddForce(Vector2.up * jumpForce); + OnFall.Invoke(); + } + + + void HandleFall() + { + Debug.Log("Falling"); + isGrounded = false; + } + + void HandleLand() + { + Debug.Log("Landing"); + isGrounded = true; + jumpCount = maxJumpCount; + } +} diff --git a/Assets/Scripts/CharacterController2D.cs.meta b/Assets/Scripts/CharacterController2D.cs.meta new file mode 100644 index 0000000..1f71f96 --- /dev/null +++ b/Assets/Scripts/CharacterController2D.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eb4a7477527c77541b25fea159d6b4c8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/FollowCamera.cs b/Assets/Scripts/FollowCamera.cs new file mode 100644 index 0000000..5b58766 --- /dev/null +++ b/Assets/Scripts/FollowCamera.cs @@ -0,0 +1,32 @@ +using System.Collections; +using UnityEngine; + +public class FollowCamera : MonoBehaviour +{ + public float speed = 15f; + public float minDistance; + public GameObject target; + public Vector3 offset; + + private Vector3 targetPos; + + // Use this for initialization + void Start() + { + targetPos = transform.position; + } + + // Update is called once per frame + void Update() + { + if (target) + { + Vector3 posNoZ = transform.position + offset; + Vector3 targetDirection = (target.transform.position - posNoZ); + float interpVelocity = targetDirection.magnitude * speed; + targetPos = (transform.position) + (targetDirection.normalized * interpVelocity * Time.deltaTime); + + + } + } +}
\ No newline at end of file diff --git a/Assets/Scripts/FollowCamera.cs.meta b/Assets/Scripts/FollowCamera.cs.meta new file mode 100644 index 0000000..cc6627c --- /dev/null +++ b/Assets/Scripts/FollowCamera.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f2db6b4344b743a428dae55cd1303cfb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/InfiniteBackground.cs b/Assets/Scripts/InfiniteBackground.cs new file mode 100644 index 0000000..d587040 --- /dev/null +++ b/Assets/Scripts/InfiniteBackground.cs @@ -0,0 +1,41 @@ +using System.Collections; +using UnityEngine; + +public class InfiniteBackground : MonoBehaviour +{ + [SerializeField] GameObject cam; + [SerializeField] float parallaxEffect = 0.3f; + + internal Vector2 pos; + internal float spriteExtentX; + + void Start() + { + Vector2 pos = transform.position; + spriteExtentX = GetComponent<SpriteRenderer>().bounds.size.x / 2; + } + + void FixedUpdate() + { + float camExtentX = Camera.main.orthographicSize * Screen.width / Screen.height; + float newPosX = pos.x + (cam.transform.position.x * parallaxEffect); + + if (newPosX + spriteExtentX <= Camera.main.transform.position.x + camExtentX) + { + var clone = Instantiate(this.gameObject); + clone.transform.position = new Vector3(newPosX + spriteExtentX, transform.position.y, transform.position.z); + } + else if (newPosX - spriteExtentX >= Camera.main.transform.position.x - camExtentX) + { + var clone = Instantiate(gameObject); + clone.transform.position = new Vector3(newPosX - spriteExtentX, transform.position.y, transform.position.z); + } + + transform.position = new Vector3(newPosX, transform.position.y, transform.position.z); + } + + void OnBecameInvisible() + { + Destroy(gameObject); + } +}
\ No newline at end of file diff --git a/Assets/Scripts/InfiniteBackground.cs.meta b/Assets/Scripts/InfiniteBackground.cs.meta new file mode 100644 index 0000000..e80bc47 --- /dev/null +++ b/Assets/Scripts/InfiniteBackground.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 78cf4261365d6b3418063f289c6881bb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |