Skip to content

Commit

Permalink
Add basic PlayerController script
Browse files Browse the repository at this point in the history
  • Loading branch information
PingGames committed Feb 22, 2023
1 parent f9d4a7d commit 074bfc9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
56 changes: 53 additions & 3 deletions src/Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ GameObject:
- component: {fileID: 564520040}
- component: {fileID: 564520039}
- component: {fileID: 564520038}
m_Layer: 0
m_Layer: 3
m_Name: Ground
m_TagString: Untagged
m_Icon: {fileID: 0}
Expand Down Expand Up @@ -475,6 +475,7 @@ GameObject:
- component: {fileID: 1269992424}
- component: {fileID: 1269992423}
- component: {fileID: 1269992422}
- component: {fileID: 1269992426}
m_Layer: 0
m_Name: Player
m_TagString: Untagged
Expand Down Expand Up @@ -502,7 +503,7 @@ Rigidbody2D:
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 0
m_Constraints: 4
--- !u!70 &1269992423
CapsuleCollider2D:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -582,7 +583,56 @@ Transform:
m_LocalPosition: {x: -1.1765958, y: 1.3683718, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Children:
- {fileID: 1970047959}
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1269992426
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1269992421}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e279b2124c44cd549ae23b0b0ad5db32, type: 3}
m_Name:
m_EditorClassIdentifier:
_rb: {fileID: 1269992422}
_groundCheck: {fileID: 1970047959}
_groundLayer:
serializedVersion: 2
m_Bits: 8
--- !u!1 &1970047958
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1970047959}
m_Layer: 0
m_Name: GroundCheck
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1970047959
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1970047958}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: -1.02, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1269992425}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
39 changes: 39 additions & 0 deletions src/Assets/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
private float _horizontal;
private float _speed = 8f;
private float _jumpingPower = 16f;

[SerializeField] private Rigidbody2D _rb;
[SerializeField] private Transform _groundCheck;
[SerializeField] private LayerMask _groundLayer;

void Update()
{
_horizontal = Input.GetAxisRaw("Horizontal");

if (Input.GetButtonDown("Jump") && IsGrounded())
{
_rb.velocity = new Vector2(_rb.velocity.x, _jumpingPower);
}

if (Input.GetButtonDown("Jump") && _rb.velocity.y > 0f)
{
_rb.velocity = new Vector2(_rb.velocity.x, _rb.velocity.y * 0.5f);
}
}

private void FixedUpdate()
{
_rb.velocity = new Vector2(_horizontal * _speed, _rb.velocity.y);
}

private bool IsGrounded()
{
return Physics2D.OverlapCircle(_groundCheck.position, 0.2f, _groundLayer);
}
}
11 changes: 11 additions & 0 deletions src/Assets/Scripts/PlayerController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ProjectSettings/TagManager.asset
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TagManager:
- Default
- TransparentFX
- Ignore Raycast
-
- Ground
- Water
- UI
-
Expand Down

0 comments on commit 074bfc9

Please sign in to comment.