This commit is contained in:
2026-03-29 02:26:31 +07:00
parent 1e458a4f09
commit 99c70886a5
22 changed files with 2430 additions and 44 deletions
+31
View File
@@ -0,0 +1,31 @@
using UnityEngine;
namespace InfiniteWorld
{
public class CameraFollow2D : MonoBehaviour
{
[SerializeField] private Transform target;
[SerializeField] private float smoothTime = 0.18f;
[SerializeField] private Vector3 offset = new Vector3(0f, 0f, -10f);
private Vector3 velocity;
private void LateUpdate()
{
if (target == null)
{
SimplePlayerInputMover player = FindFirstObjectByType<SimplePlayerInputMover>();
if (player == null)
{
return;
}
target = player.transform;
}
Vector3 desiredPosition = target.position + offset;
desiredPosition.z = offset.z;
transform.position = Vector3.SmoothDamp(transform.position, desiredPosition, ref velocity, smoothTime);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 98ee4fb5b3ebf80478e6e25afa8fd337
@@ -0,0 +1,109 @@
using UnityEngine;
using UnityEngine.InputSystem;
namespace InfiniteWorld
{
public class SimplePlayerInputMover : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f;
private InputAction moveAction;
private Rigidbody2D rb;
private Vector2 moveInput;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
if (rb == null)
{
rb = gameObject.AddComponent<Rigidbody2D>();
}
ConfigurePhysics();
EnsureVisual();
moveAction = new InputAction("Move", InputActionType.Value);
moveAction.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
moveAction.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/upArrow")
.With("Down", "<Keyboard>/downArrow")
.With("Left", "<Keyboard>/leftArrow")
.With("Right", "<Keyboard>/rightArrow");
moveAction.AddBinding("<Gamepad>/leftStick");
}
private void OnEnable()
{
moveAction?.Enable();
}
private void OnDisable()
{
moveAction?.Disable();
}
private void OnDestroy()
{
moveAction?.Dispose();
}
private void Update()
{
if (moveAction == null)
{
return;
}
moveInput = moveAction.ReadValue<Vector2>().normalized;
if (moveInput.x != 0f)
{
Vector3 scale = transform.localScale;
scale.x = Mathf.Abs(scale.x) * Mathf.Sign(moveInput.x);
transform.localScale = scale;
}
}
private void FixedUpdate()
{
if (rb == null)
{
return;
}
Vector2 target = rb.position + moveInput * (moveSpeed * Time.fixedDeltaTime);
rb.MovePosition(target);
}
private void ConfigurePhysics()
{
rb.gravityScale = 0f;
rb.freezeRotation = true;
rb.interpolation = RigidbodyInterpolation2D.Interpolate;
rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
CapsuleCollider2D collider = GetComponent<CapsuleCollider2D>();
if (collider == null)
{
collider = gameObject.AddComponent<CapsuleCollider2D>();
}
collider.direction = CapsuleDirection2D.Vertical;
collider.size = new Vector2(0.55f, 0.8f);
collider.offset = new Vector2(0f, -0.05f);
}
private void EnsureVisual()
{
SpriteRenderer renderer = GetComponent<SpriteRenderer>();
if (renderer == null)
{
renderer = gameObject.AddComponent<SpriteRenderer>();
}
renderer.sprite = ProceduralWorldArt.CreatePlayerSprite();
renderer.sortingOrder = 10;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c9187e81c6ec8da4599e04a1694ec18b