Adding character movement basics
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -130,6 +130,7 @@ GameObject:
|
||||
- component: {fileID: 100100001}
|
||||
- component: {fileID: 100100002}
|
||||
- component: {fileID: 100100003}
|
||||
- component: {fileID: 100100004}
|
||||
m_Layer: 0
|
||||
m_Name: Ground
|
||||
m_TagString: Untagged
|
||||
@@ -223,6 +224,29 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 13a3649e0bdf48b4afa233a4a4b64820, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!61 &100100004
|
||||
BoxCollider2D:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 100100000}
|
||||
m_Enabled: 1
|
||||
m_Density: 1
|
||||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_IsTrigger: 0
|
||||
m_UsedByEffector: 0
|
||||
m_UsedByComposite: 0
|
||||
m_CompositeOperation: 0
|
||||
m_Offset: {x: 0, y: 0}
|
||||
m_Size: {x: 1, y: 1}
|
||||
m_EdgeRadius: 0
|
||||
--- !u!1 &100200000
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -234,6 +258,9 @@ GameObject:
|
||||
- component: {fileID: 100200001}
|
||||
- component: {fileID: 100200002}
|
||||
- component: {fileID: 100200003}
|
||||
- component: {fileID: 100200004}
|
||||
- component: {fileID: 100200005}
|
||||
- component: {fileID: 100200006}
|
||||
m_Layer: 0
|
||||
m_Name: Player
|
||||
m_TagString: Untagged
|
||||
@@ -327,6 +354,76 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 13a3649e0bdf48b4afa233a4a4b64820, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!50 &100200004
|
||||
Rigidbody2D:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 100200000}
|
||||
serializedVersion: 4
|
||||
m_BodyType: 0
|
||||
m_Simulated: 1
|
||||
m_UseFullKinematicContacts: 0
|
||||
m_UseAutoMass: 0
|
||||
m_Mass: 1
|
||||
m_LinearDrag: 0
|
||||
m_AngularDrag: 0.05
|
||||
m_GravityScale: 1
|
||||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_Interpolation: 0
|
||||
m_SleepingMode: 1
|
||||
m_CollisionDetection: 0
|
||||
m_Constraints: 4
|
||||
--- !u!61 &100200005
|
||||
BoxCollider2D:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 100200000}
|
||||
m_Enabled: 1
|
||||
m_Density: 1
|
||||
m_Material: {fileID: 0}
|
||||
m_IncludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_ExcludeLayers:
|
||||
serializedVersion: 2
|
||||
m_Bits: 0
|
||||
m_IsTrigger: 0
|
||||
m_UsedByEffector: 0
|
||||
m_UsedByComposite: 0
|
||||
m_CompositeOperation: 0
|
||||
m_Offset: {x: 0, y: 0}
|
||||
m_Size: {x: 1, y: 1}
|
||||
m_EdgeRadius: 0
|
||||
--- !u!114 &100200006
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 100200000}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 528643a4ae944836a40ce783da8c1b78, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
inputActions: {fileID: 11400000, guid: 2bcd2660ca9b64942af0de543d8d7100, type: 2}
|
||||
actionMapName: Player
|
||||
moveActionName: Move
|
||||
jumpActionName: Jump
|
||||
moveSpeed: 6
|
||||
jumpImpulse: 8
|
||||
groundMask: -1
|
||||
groundCheckDistance: 0.05
|
||||
--- !u!1 &855644964
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(BoxCollider2D))]
|
||||
public class PlayerPlatformerController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private InputActionAsset inputActions;
|
||||
[SerializeField] private string actionMapName = "Player";
|
||||
[SerializeField] private string moveActionName = "Move";
|
||||
[SerializeField] private string jumpActionName = "Jump";
|
||||
[SerializeField] private float moveSpeed = 6f;
|
||||
[SerializeField] private float jumpImpulse = 8f;
|
||||
[SerializeField] private LayerMask groundMask = ~0;
|
||||
[SerializeField] private float groundCheckDistance = 0.05f;
|
||||
|
||||
private Rigidbody2D body;
|
||||
private BoxCollider2D bodyCollider;
|
||||
private InputAction moveAction;
|
||||
private InputAction jumpAction;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
body = GetComponent<Rigidbody2D>();
|
||||
bodyCollider = GetComponent<BoxCollider2D>();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (inputActions == null)
|
||||
{
|
||||
Debug.LogError("PlayerPlatformerController missing InputActionAsset reference.", this);
|
||||
return;
|
||||
}
|
||||
|
||||
var map = inputActions.FindActionMap(actionMapName, true);
|
||||
moveAction = map.FindAction(moveActionName, true);
|
||||
jumpAction = map.FindAction(jumpActionName, true);
|
||||
|
||||
moveAction.Enable();
|
||||
jumpAction.Enable();
|
||||
jumpAction.performed += OnJumpPerformed;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (jumpAction != null)
|
||||
{
|
||||
jumpAction.performed -= OnJumpPerformed;
|
||||
jumpAction.Disable();
|
||||
}
|
||||
|
||||
if (moveAction != null)
|
||||
{
|
||||
moveAction.Disable();
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (moveAction == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var input = moveAction.ReadValue<Vector2>();
|
||||
var velocity = body.linearVelocity;
|
||||
velocity.x = input.x * moveSpeed;
|
||||
body.linearVelocity = velocity;
|
||||
}
|
||||
|
||||
private void OnJumpPerformed(InputAction.CallbackContext context)
|
||||
{
|
||||
if (!IsGrounded())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var velocity = body.linearVelocity;
|
||||
velocity.y = 0f;
|
||||
body.linearVelocity = velocity;
|
||||
body.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse);
|
||||
}
|
||||
|
||||
private bool IsGrounded()
|
||||
{
|
||||
var bounds = bodyCollider.bounds;
|
||||
var checkSize = new Vector2(bounds.size.x * 0.9f, groundCheckDistance * 2f);
|
||||
var checkCenter = new Vector2(bounds.center.x, bounds.min.y - groundCheckDistance);
|
||||
var hits = Physics2D.OverlapBoxAll(checkCenter, checkSize, 0f, groundMask);
|
||||
for (var i = 0; i < hits.Length; i++)
|
||||
{
|
||||
var hit = hits[i];
|
||||
if (hit != null && hit != bodyCollider)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 528643a4ae944836a40ce783da8c1b78
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -682,11 +682,11 @@ PlayerSettings:
|
||||
captureStartupLogs: {}
|
||||
activeInputHandler: 1
|
||||
windowsGamepadBackendHint: 0
|
||||
cloudProjectId:
|
||||
cloudProjectId: b260f126-ec58-47f4-b454-ba7db288a3bf
|
||||
framebufferDepthMemorylessMode: 0
|
||||
qualitySettingsNames: []
|
||||
projectName:
|
||||
organizationId:
|
||||
projectName: GGJ2026
|
||||
organizationId: fa85ebd-8959-4e73-a927-ec9a8c20a21e
|
||||
cloudEnabled: 0
|
||||
legacyClampBlendShapeWeights: 0
|
||||
hmiLoadingImage: {fileID: 0}
|
||||
|
||||
Reference in New Issue
Block a user