Skip to content

Commit

Permalink
Fixed raycast that check ground to check by rayDirection.
Browse files Browse the repository at this point in the history
  • Loading branch information
CSaratakij committed Nov 11, 2018
1 parent da9dbaf commit b7d5d65
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
16 changes: 15 additions & 1 deletion Assets/Prefabs/Player/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ GameObject:
- component: {fileID: 61496652745040014}
- component: {fileID: 58157547062579972}
- component: {fileID: 114091297035214810}
- component: {fileID: 114550054155471720}
m_Layer: 8
m_Name: Player
m_TagString: Untagged
Expand Down Expand Up @@ -147,11 +148,24 @@ MonoBehaviour:
moveForce: 360
jumpForce: 700
gravity: 30
maxVerticalForce: 550
terminalVelocity: 550
ground: {fileID: 4483366956249144}
groundLayer:
serializedVersion: 2
m_Bits: 1
--- !u!114 &114550054155471720
MonoBehaviour:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1177175208556038}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ccf9a90a341a6734a9d960cab7d812c3, type: 3}
m_Name:
m_EditorClassIdentifier:
current: 4
max: 100
--- !u!212 &212542139723826972
SpriteRenderer:
m_ObjectHideFlags: 1
Expand Down
18 changes: 3 additions & 15 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -8250,24 +8250,12 @@ Prefab:
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 1b6220d3d67bd4b4c8c1519c076f8038, type: 2}
m_IsPrefabAsset: 0
--- !u!1 &1182614325 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 1177175208556038, guid: 1b6220d3d67bd4b4c8c1519c076f8038,
--- !u!114 &1182614326 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 114550054155471720, guid: 1b6220d3d67bd4b4c8c1519c076f8038,
type: 2}
m_PrefabInternal: {fileID: 1182614324}
--- !u!114 &1182614326
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1182614325}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ccf9a90a341a6734a9d960cab7d812c3, type: 3}
m_Name:
m_EditorClassIdentifier:
current: 4
max: 100
--- !u!1 &1185556008
GameObject:
m_ObjectHideFlags: 0
Expand Down
8 changes: 5 additions & 3 deletions Assets/Scripts/Player/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PlayerController : MonoBehaviour
float gravity;

[SerializeField]
float maxVerticalForce;
float terminalVelocity;

[SerializeField]
Transform ground;
Expand All @@ -27,6 +27,7 @@ public class PlayerController : MonoBehaviour

Vector2 inputVector;
Vector2 velocity;
Vector2 rayDirection;

RaycastHit2D hit;
Rigidbody2D rigid;
Expand Down Expand Up @@ -54,6 +55,7 @@ void Initialize()
{
isPressedJump = false;
isCanJump = false;
rayDirection = new Vector2(1.0f, -1.0f);
rigid = GetComponent<Rigidbody2D>();
health = GetComponent<Status>();
}
Expand All @@ -71,7 +73,7 @@ void InputHandler()

void CheckGround()
{
hit = Physics2D.Raycast(ground.position, ground.right, 0.2f, groundLayer);
hit = Physics2D.Raycast(ground.position, rayDirection, 0.2f, groundLayer);
isCanJump = (hit.collider != null);
}

Expand All @@ -84,7 +86,7 @@ void MovementHandler()
}
else {
velocity.y -= gravity;
velocity.y = Mathf.Clamp(velocity.y, -maxVerticalForce, jumpForce);
velocity.y = Mathf.Clamp(velocity.y, -terminalVelocity, jumpForce);
}

rigid.velocity = (velocity * Time.deltaTime);
Expand Down

0 comments on commit b7d5d65

Please sign in to comment.