Commit ec17dc00 authored by Martin Mihálik's avatar Martin Mihálik 🔥
Browse files

Fixed Movement

parent 98cc0ee5
......@@ -269,12 +269,23 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b691a2a756a6e9a4a9f82d992f114b5b, type: 3}
m_Name:
m_EditorClassIdentifier:
m_JumpForce: 600
m_MovementSmoothing: 0.05
m_JumpForce: 400
m_MovementSpeed: 500
m_AirControl: 1
m_WhatIsGround:
serializedVersion: 2
m_Bits: 8
m_Filter:
useTriggers: 0
useLayerMask: 1
useDepth: 0
useOutsideDepth: 0
useNormalAngle: 0
useOutsideNormalAngle: 0
layerMask:
serializedVersion: 2
m_Bits: 8
minDepth: 0
maxDepth: 0
minNormalAngle: 0
maxNormalAngle: 0
--- !u!114 &512397506
MonoBehaviour:
m_ObjectHideFlags: 0
......@@ -287,7 +298,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 384f8f188b664b745bc1f34d36ce528e, type: 3}
m_Name:
m_EditorClassIdentifier:
m_MovementSpeed: 500
--- !u!1 &519420028
GameObject:
m_ObjectHideFlags: 0
......@@ -427,6 +437,8 @@ GameObject:
- component: {fileID: 1877781304}
- component: {fileID: 1877781303}
- component: {fileID: 1877781305}
- component: {fileID: 1877781307}
- component: {fileID: 1877781306}
m_Layer: 3
m_Name: Tilemap
m_TagString: Untagged
......@@ -1047,10 +1059,91 @@ TilemapCollider2D:
m_GameObject: {fileID: 1877781301}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_Material: {fileID: 6200000, guid: 9585bd1897407494d9d3063e1a79010f, type: 2}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_UsedByComposite: 1
m_Offset: {x: 0, y: 0}
m_MaximumTileChangeCount: 1000
m_ExtrusionFactor: 0.00001
--- !u!66 &1877781306
CompositeCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1877781301}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_GeometryType: 0
m_GenerationType: 0
m_EdgeRadius: 0
m_ColliderPaths:
- m_Collider: {fileID: 1877781305}
m_ColliderPaths:
- - X: -20000000
Y: -20000000
- X: -260000000
Y: -20000000
- X: -260000000
Y: -30000000
- X: -20000000
Y: -30000000
- - X: 260000000
Y: -20000000
- X: 210000100
Y: -20000000
- X: 210000100
Y: -20000041
- X: 210000041
Y: -20000100
- X: 179999959
Y: -20000100
- X: 179999900
Y: -20000041
- X: 179999900
Y: -20000000
- X: 20000000
Y: -20000000
- X: 20000000
Y: -30000000
- X: 260000000
Y: -30000000
m_CompositePaths:
m_Paths:
- - {x: 25.999971, y: -3}
- {x: 25.999971, y: -2}
- {x: 2, y: -2.0000293}
- {x: 2.0000293, y: -3}
- - {x: -2.0000293, y: -3}
- {x: -2.0000293, y: -2}
- {x: -26, y: -2.0000293}
- {x: -25.999971, y: -3}
m_VertexDistance: 0.0005
m_OffsetDistance: 0.00005
--- !u!50 &1877781307
Rigidbody2D:
serializedVersion: 4
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1877781301}
m_BodyType: 2
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_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 0
......@@ -4,26 +4,13 @@ public class PlayerControls : MonoBehaviour
{
private PlayerMovement m_Controller;
private bool m_Jump = false;
private float m_HorizontalMove = 0f;
[SerializeField]
private float m_MovementSpeed = 100f;
private void Start()
{
m_Controller = GetComponent<PlayerMovement>();
}
private void Update()
{
m_HorizontalMove = Input.GetAxisRaw("Horizontal");
m_Jump = Input.GetButtonDown("Jump");
}
private void FixedUpdate()
{
m_Controller.Move(m_HorizontalMove * m_MovementSpeed * Time.fixedDeltaTime, m_Jump);
m_Jump = false;
m_Controller.Move(Input.GetAxis("Horizontal"), Input.GetButton("Jump"));
}
}
......@@ -2,42 +2,33 @@ using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float m_JumpForce = 400f;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
[SerializeField] private bool m_AirControl = false;
[SerializeField] private LayerMask m_WhatIsGround;
private bool m_Grounded;
private Vector3 m_Velocity = Vector3.zero;
private Rigidbody2D m_Rigidbody2D;
private BoxCollider2D m_Collider;
private void Start()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
m_Collider = GetComponent<BoxCollider2D>();
}
private void FixedUpdate()
{
var hit = Physics2D.BoxCast(m_Collider.bounds.center, m_Collider.bounds.size, 0f, Vector2.down, m_Collider.bounds.extents.y + .1f, m_WhatIsGround);
m_Grounded = hit.collider != null;
}
public void Move(float move, bool jump)
{
if (m_Grounded || m_AirControl)
{
Vector3 targetVelocity = new Vector2(move, m_Rigidbody2D.velocity.y);
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
}
if (m_Grounded && jump)
{
m_Grounded = false;
m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
}
}
[SerializeField] private float m_JumpForce = 400f;
[SerializeField] private float m_MovementSpeed = 0f;
[SerializeField] private bool m_AirControl = false;
[SerializeField] private ContactFilter2D m_Filter;
private Rigidbody2D m_Rigidbody2D;
private void Start()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
}
public void Move(float direction, bool jump)
{
var grounded = m_Rigidbody2D.IsTouching(m_Filter);
if (grounded || m_AirControl)
{
var horizontalSpeed = direction * m_MovementSpeed * Time.fixedDeltaTime;
var verticalSpeed = m_Rigidbody2D.velocity.y;
m_Rigidbody2D.velocity = new Vector2(horizontalSpeed, verticalSpeed);
}
if (grounded && jump)
{
m_Rigidbody2D.AddForce(Vector2.up * m_JumpForce);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment