Make platformer player movement 100% physics-based
This commit is contained in:
14
Assets/Games/Platformer/Ground.physicsMaterial2D
Normal file
14
Assets/Games/Platformer/Ground.physicsMaterial2D
Normal file
@@ -0,0 +1,14 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!62 &6200000
|
||||
PhysicsMaterial2D:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: Ground
|
||||
serializedVersion: 2
|
||||
friction: 0
|
||||
bounciness: 0
|
||||
m_FrictionCombine: 1
|
||||
m_BounceCombine: 4
|
||||
8
Assets/Games/Platformer/Ground.physicsMaterial2D.meta
Normal file
8
Assets/Games/Platformer/Ground.physicsMaterial2D.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c29c2a30d0b3d4a9b95031c5dfbd222a
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 6200000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -187,11 +187,13 @@ MonoBehaviour:
|
||||
serializedVersion: 2
|
||||
m_Bits: 64
|
||||
walkSpeed: 5
|
||||
walkSpeedFactor: 30
|
||||
maxSpeed: 5
|
||||
virtualAxisX: 0
|
||||
virtualButtonJump: 0
|
||||
virtualButtonJumpLastFrame: 0
|
||||
turnaroundMultiplier: 2
|
||||
walkSmooth: 0
|
||||
walkSmooth: 0.98
|
||||
secondsToFullSpeed: 0
|
||||
jumpSpeed: 17
|
||||
coyoteTime: 0.1
|
||||
|
||||
@@ -14,6 +14,8 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
[Header("Movement")]
|
||||
public float walkSpeed;
|
||||
public float walkSpeedFactor = 1f;
|
||||
public float maxSpeed = 5f;
|
||||
public float virtualAxisX;
|
||||
public float virtualButtonJump;
|
||||
public float virtualButtonJumpLastFrame;
|
||||
@@ -65,7 +67,6 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
HorizontalMovement();
|
||||
|
||||
|
||||
Land();
|
||||
}
|
||||
|
||||
@@ -123,7 +124,18 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
private void HorizontalMovement()
|
||||
{
|
||||
body.linearVelocity = new Vector2(virtualAxisX * walkSpeed, body.linearVelocity.y);
|
||||
//body.linearVelocity = new Vector2(virtualAxisX * walkSpeed, body.linearVelocity.y);
|
||||
body.AddForce(new Vector2(virtualAxisX * walkSpeed * walkSpeedFactor, 0), ForceMode2D.Force);
|
||||
|
||||
if (Mathf.Abs(body.linearVelocityX) >= maxSpeed)
|
||||
{
|
||||
body.linearVelocity = new Vector2(Mathf.Sign(body.linearVelocityX) * maxSpeed, body.linearVelocity.y);
|
||||
}
|
||||
|
||||
//if (!IsPhysicallyGrounded())
|
||||
//{
|
||||
body.linearVelocityX *= walkSmooth;
|
||||
//}
|
||||
|
||||
if (transform.position == positionLastFrame && (InputSystem.actions.FindAction($"Player {player} Move").ReadValue<Vector2>().x == 0))
|
||||
{
|
||||
@@ -135,6 +147,12 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
private void UpdateVirtualAxis()
|
||||
{
|
||||
virtualButtonJump = InputSystem.actions.FindAction($"Player {player} Action").ReadValue<float>();
|
||||
virtualButtonJumpLastFrame = InputSystem.actions.FindAction($"Player {player} Action").WasPressedThisFrame() ? 1 : 0;
|
||||
|
||||
virtualAxisX = InputSystem.actions.FindAction($"Player {player} Move").ReadValue<Vector2>().x;
|
||||
return;
|
||||
|
||||
// From https://discussions.unity.com/t/manually-smooth-input-getaxisraw/225141/4
|
||||
float basicallyRawAxis = InputSystem.actions.FindAction($"Player {player} Move").ReadValue<Vector2>().x;
|
||||
float sensitivity = 3;
|
||||
@@ -158,9 +176,6 @@ public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
turnaroundMultiplier = 1;
|
||||
}
|
||||
|
||||
virtualButtonJump = InputSystem.actions.FindAction($"Player {player} Action").ReadValue<float>();
|
||||
virtualButtonJumpLastFrame = InputSystem.actions.FindAction($"Player {player} Action").WasPressedThisFrame() ? 1 : 0;
|
||||
}
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
{
|
||||
|
||||
14
Assets/Games/Platformer/Player.physicsMaterial2D
Normal file
14
Assets/Games/Platformer/Player.physicsMaterial2D
Normal file
@@ -0,0 +1,14 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!62 &6200000
|
||||
PhysicsMaterial2D:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: Player
|
||||
serializedVersion: 2
|
||||
friction: 0
|
||||
bounciness: 0
|
||||
m_FrictionCombine: 1
|
||||
m_BounceCombine: 4
|
||||
8
Assets/Games/Platformer/Player.physicsMaterial2D.meta
Normal file
8
Assets/Games/Platformer/Player.physicsMaterial2D.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 522e8bb901c31496c91921bd603c0e8b
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 6200000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user