fixed player bugs
This commit is contained in:
@@ -3,10 +3,20 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.Model
|
||||
{
|
||||
public static class TileToKeyMappings
|
||||
{
|
||||
public static List<KeyCode> TileToKey = new List<KeyCode>
|
||||
{
|
||||
KeyCode.Q,
|
||||
KeyCode.W,
|
||||
KeyCode.E,
|
||||
KeyCode.A,
|
||||
KeyCode.S,
|
||||
KeyCode.D
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -470,7 +470,7 @@ Transform:
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 1146013793}
|
||||
m_RootOrder: 2
|
||||
m_RootOrder: 1
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!212 &367096170
|
||||
SpriteRenderer:
|
||||
@@ -952,8 +952,9 @@ MonoBehaviour:
|
||||
runSpeed: 10
|
||||
jumpSpeed: 35
|
||||
runFasterFactor: 1.5
|
||||
groundErrorThreshold: 3
|
||||
wallLineCaseDistance: 1
|
||||
groundErrorThreshold: 1.5
|
||||
wallErrorThreshold: 1.6
|
||||
wallLineCaseDistance: 0
|
||||
runErrorThreshold: 0.05
|
||||
--- !u!95 &1671840923
|
||||
Animator:
|
||||
@@ -1228,28 +1229,27 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 555a3f8aa89a0e84fb6c43249a117f93, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
player: {fileID: 1671840921}
|
||||
camera: {fileID: 1337282922}
|
||||
tile: {fileID: 11400000, guid: 1f7bfa5ee72002646806268d14bfeb3b, type: 2}
|
||||
tiles:
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 11400000, guid: 1f7bfa5ee72002646806268d14bfeb3b, type: 2}
|
||||
- {fileID: 11400000, guid: c2678bd928e9a0943ad5fadb602db819, type: 2}
|
||||
- {fileID: 11400000, guid: e85f8319cdf4358448a9002845cfbd5a, type: 2}
|
||||
- {fileID: 11400000, guid: db1d65b4da209fa489ba3d7582dbff24, type: 2}
|
||||
- {fileID: 11400000, guid: daea938cbd864e0479f663f746bb9950, type: 2}
|
||||
- {fileID: 11400000, guid: 4c5438b1315e8d840b6c482e9710993c, type: 2}
|
||||
successTiles:
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
- {fileID: 0}
|
||||
successTile: {fileID: 11400000, guid: ae6e7a8b9fa2433448a66da4090e310c, type: 2}
|
||||
- {fileID: 11400000, guid: ae6e7a8b9fa2433448a66da4090e310c, type: 2}
|
||||
- {fileID: 11400000, guid: 2144cee47e8929f4cb0ce23b78f8f4ad, type: 2}
|
||||
- {fileID: 11400000, guid: ccb4ffcdbc096b448881b3a12ed1bfce, type: 2}
|
||||
- {fileID: 11400000, guid: cb7c540b9df3b994fb4320684eb0ff65, type: 2}
|
||||
- {fileID: 11400000, guid: f3b494a9dede1e34cbf612b95ee136ec, type: 2}
|
||||
- {fileID: 11400000, guid: e3c7dcade26bec746bd38fb794264216, type: 2}
|
||||
plusTile: {fileID: 11400000, guid: ee14e10b7ac07bd449e0f1d5ea99eb42, type: 2}
|
||||
framesPerControlTile: 15
|
||||
framesPerControlTile: 8
|
||||
nextControlYOffset: 1
|
||||
nextControlXOffset: -1
|
||||
tileMapClearDelayInSeconds: 0.5
|
||||
tileMapClearDelayInSeconds: 0.3
|
||||
maxNumberOfControlTiles: 1
|
||||
--- !u!483693784 &1759518266
|
||||
TilemapRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -1369,86 +1369,6 @@ Tilemap:
|
||||
e31: 0
|
||||
e32: 0
|
||||
e33: 1
|
||||
--- !u!1 &1882958979
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1882958980}
|
||||
- component: {fileID: 1882958981}
|
||||
m_Layer: 0
|
||||
m_Name: neovera-service-bg
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 0
|
||||
--- !u!4 &1882958980
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1882958979}
|
||||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
|
||||
m_LocalPosition: {x: -9.46, y: 1.25, z: 10}
|
||||
m_LocalScale: {x: 5.4678884, y: 5.4678884, z: 5.4678884}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 1146013793}
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!212 &1882958981
|
||||
SpriteRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1882958979}
|
||||
m_Enabled: 1
|
||||
m_CastShadows: 0
|
||||
m_ReceiveShadows: 0
|
||||
m_DynamicOccludee: 1
|
||||
m_MotionVectors: 1
|
||||
m_LightProbeUsage: 1
|
||||
m_ReflectionProbeUsage: 1
|
||||
m_RenderingLayerMask: 1
|
||||
m_RendererPriority: 0
|
||||
m_Materials:
|
||||
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
|
||||
m_StaticBatchInfo:
|
||||
firstSubMesh: 0
|
||||
subMeshCount: 0
|
||||
m_StaticBatchRoot: {fileID: 0}
|
||||
m_ProbeAnchor: {fileID: 0}
|
||||
m_LightProbeVolumeOverride: {fileID: 0}
|
||||
m_ScaleInLightmap: 1
|
||||
m_ReceiveGI: 1
|
||||
m_PreserveUVs: 0
|
||||
m_IgnoreNormalsForChartDetection: 0
|
||||
m_ImportantGI: 0
|
||||
m_StitchLightmapSeams: 1
|
||||
m_SelectedEditorRenderState: 0
|
||||
m_MinimumChartSize: 4
|
||||
m_AutoUVMaxDistance: 0.5
|
||||
m_AutoUVMaxAngle: 89
|
||||
m_LightmapParameters: {fileID: 0}
|
||||
m_SortingLayerID: 0
|
||||
m_SortingLayer: 0
|
||||
m_SortingOrder: -10
|
||||
m_Sprite: {fileID: 21300000, guid: 2fe41ab735eeacb4b9bf0f9b09a41805, type: 3}
|
||||
m_Color: {r: 1, g: 1, b: 1, a: 1}
|
||||
m_FlipX: 0
|
||||
m_FlipY: 0
|
||||
m_DrawMode: 0
|
||||
m_Size: {x: 7, y: 3.28}
|
||||
m_AdaptiveModeThreshold: 0.5
|
||||
m_SpriteTileMode: 0
|
||||
m_WasSpriteAssigned: 1
|
||||
m_MaskInteraction: 0
|
||||
m_SpriteSortPoint: 0
|
||||
--- !u!1 &2086987803
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -1478,7 +1398,7 @@ Transform:
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 1146013793}
|
||||
m_RootOrder: 1
|
||||
m_RootOrder: 0
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!212 &2086987805
|
||||
SpriteRenderer:
|
||||
|
||||
@@ -7,22 +7,21 @@ using UnityEngine.Tilemaps;
|
||||
|
||||
public class EventControls : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Player player;
|
||||
[SerializeField] private Camera camera;
|
||||
[SerializeField] private Tile tile;
|
||||
[SerializeField] private List<Tile> tiles;
|
||||
[SerializeField] private List<Tile> successTiles;
|
||||
[SerializeField] private Tile successTile;
|
||||
[SerializeField] private Tile plusTile;
|
||||
[SerializeField] private float framesPerControlTile = 15;
|
||||
[SerializeField] private int nextControlYOffset = 0;
|
||||
[SerializeField] private int nextControlXOffset = 0;
|
||||
private int nextControlXOffset => 0 - maxNumberOfControlTiles + 1;
|
||||
[SerializeField] private float tileMapClearDelayInSeconds = 0.5f;
|
||||
[SerializeField] private int maxNumberOfControlTiles = 2;
|
||||
|
||||
private Tilemap tilemap;
|
||||
|
||||
/* For drawing event control tiles */
|
||||
private Vector3Int? initialCameraPosition;
|
||||
private int maxNumberOfControlTiles = 3;
|
||||
private int numberOfControlTilesSet = 0;
|
||||
private int nextControlAccumalator = 0;
|
||||
|
||||
@@ -47,6 +46,8 @@ public class EventControls : MonoBehaviour
|
||||
{
|
||||
currentPlayerCollider = playerCollider;
|
||||
currentEnemyCollider = enemyCollider;
|
||||
|
||||
player.DisablePlayerMovement();
|
||||
Physics2D.IgnoreCollision(playerCollider, enemyCollider, true);
|
||||
|
||||
eventTriggered = true;
|
||||
@@ -54,7 +55,6 @@ public class EventControls : MonoBehaviour
|
||||
|
||||
public bool IsEventTriggered()
|
||||
{
|
||||
|
||||
return eventTriggered;
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public class EventControls : MonoBehaviour
|
||||
if (eventTriggered)
|
||||
{
|
||||
SetTile();
|
||||
//SetTileMapPosition();
|
||||
// SetTileMapPosition();
|
||||
}
|
||||
|
||||
if (eventDrawn && eventTriggered)
|
||||
@@ -94,19 +94,13 @@ public class EventControls : MonoBehaviour
|
||||
EventControlTile eventControlTile = null;
|
||||
if (isControlTile == null || isControlTile.GetValueOrDefault())
|
||||
{
|
||||
eventControlTile = new EventControlTile(KeyCode.Q,
|
||||
tile,
|
||||
successTile,
|
||||
GetNextTilePosition() + nextControlOffSetAsVector); /* TODO: Hard code */
|
||||
eventControlTile = GenerateEventControlTile(GetNextTilePosition() + nextControlOffSetAsVector);
|
||||
isControlTile = false;
|
||||
numberOfControlTilesSet++;
|
||||
}
|
||||
else
|
||||
{
|
||||
eventControlTile = new EventControlTile(KeyCode.Escape,
|
||||
plusTile,
|
||||
null,
|
||||
GetNextTilePosition() + nextControlOffSetAsVector,
|
||||
true); /* TODO: Hard code */
|
||||
eventControlTile = GenerateEventControlPlusTile(GetNextPlusTilePosition() + nextControlOffSetAsVector);
|
||||
isControlTile = true;
|
||||
}
|
||||
|
||||
@@ -115,7 +109,6 @@ public class EventControls : MonoBehaviour
|
||||
tilemap.SetTile(eventControlTile.position, eventControlTile.tile);
|
||||
|
||||
nextControlAccumalator = 0;
|
||||
numberOfControlTilesSet++;
|
||||
}
|
||||
else if (numberOfControlTilesSet == maxNumberOfControlTiles)
|
||||
{
|
||||
@@ -138,11 +131,17 @@ public class EventControls : MonoBehaviour
|
||||
|
||||
private Vector3Int GetNextTilePosition()
|
||||
{
|
||||
return (initialCameraPosition + new Vector3Int(numberOfControlTilesSet, 0, 0))
|
||||
return (initialCameraPosition + new Vector3Int(numberOfControlTilesSet*2, 0, 0))
|
||||
.GetValueOrDefault();
|
||||
}
|
||||
|
||||
private void PerformControlEvent()
|
||||
private Vector3Int GetNextPlusTilePosition()
|
||||
{
|
||||
return (initialCameraPosition + new Vector3Int(numberOfControlTilesSet*2-1, 0, 0))
|
||||
.GetValueOrDefault();
|
||||
}
|
||||
|
||||
private bool PerformControlEvent()
|
||||
{
|
||||
nextControlAccumalator = 0;
|
||||
|
||||
@@ -163,24 +162,33 @@ public class EventControls : MonoBehaviour
|
||||
}
|
||||
else if (eventCycleAccumalator >= eventCycleInSeconds * framesPerSecond)
|
||||
{
|
||||
|
||||
eventTriggered = false;
|
||||
eventCycleAccumalator = 0;
|
||||
Destroy(currentEnemyCollider.gameObject);
|
||||
currentEnemyCollider = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
eventCycleAccumalator++;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
if (tileMapClearDelayAccumalator >= tileMapClearDelayInSeconds * framesPerSecond)
|
||||
{
|
||||
eventControlTilesInCycle.Clear();
|
||||
tilemap.ClearAllTiles();
|
||||
tileMapClearDelayAccumalator = 0;
|
||||
eventDrawn = false;
|
||||
numberOfControlTilesSet = 0;
|
||||
|
||||
if (currentEnemyCollider != null)
|
||||
Physics2D.IgnoreCollision(currentPlayerCollider, currentEnemyCollider, false);
|
||||
player.EnablePlayerMovement();
|
||||
|
||||
currentPlayerCollider = null;
|
||||
currentEnemyCollider = null;
|
||||
}
|
||||
@@ -190,9 +198,28 @@ public class EventControls : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void SetTileMapPosition()
|
||||
//private void SetTileMapPosition()
|
||||
//{
|
||||
// var offset = camera.transform.position.x - (int)camera.transform.position.x;
|
||||
// transform.position = new Vector3(transform.position.x + offset, transform.position.y, 0);
|
||||
//}
|
||||
|
||||
private EventControlTile GenerateEventControlTile(Vector3Int position)
|
||||
{
|
||||
transform.position = camera.transform.position +
|
||||
new Vector3Int(nextControlXOffset, nextControlYOffset, 0); // offset
|
||||
var randomNumber = Random.Range(0, TileToKeyMappings.TileToKey.Count);
|
||||
|
||||
return new EventControlTile(TileToKeyMappings.TileToKey[randomNumber],
|
||||
tiles[randomNumber],
|
||||
successTiles[randomNumber],
|
||||
position);
|
||||
}
|
||||
|
||||
private EventControlTile GenerateEventControlPlusTile(Vector3Int position)
|
||||
{
|
||||
return new EventControlTile(KeyCode.Escape,
|
||||
plusTile,
|
||||
null,
|
||||
position,
|
||||
true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,18 @@ using UnityEngine;
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float runSpeed = 10f;
|
||||
[SerializeField] private float jumpSpeed = 10f;
|
||||
[SerializeField] private float jumpSpeed = 35f;
|
||||
[SerializeField] private float runFasterFactor = 1.5f;
|
||||
[SerializeField] private float groundErrorThreshold = 0.05f;
|
||||
[SerializeField] private float groundErrorThreshold = 1.5f;
|
||||
[SerializeField] private float wallErrorThreshold = 1.5f;
|
||||
[SerializeField] private float wallLineCaseDistance = 0.5f;
|
||||
[SerializeField] private float runErrorThreshold = 0.05f;
|
||||
|
||||
|
||||
private Rigidbody2D rigidBody;
|
||||
new private BoxCollider2D collider;
|
||||
private Animator animator;
|
||||
|
||||
private Vector2 wallLineCaseDistanceVector => new Vector2(wallLineCaseDistance, 0);
|
||||
private bool isMovementEnabled = true;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -28,13 +29,39 @@ public class Player : MonoBehaviour
|
||||
private void Update()
|
||||
{
|
||||
MovePlayer();
|
||||
|
||||
//if (IsPlayerOnWall(true))
|
||||
//{
|
||||
// if (collider.sharedMaterial == null || collider.sharedMaterial.friction != 0f)
|
||||
// {
|
||||
// collider.sharedMaterial = new PhysicsMaterial2D();
|
||||
// collider.sharedMaterial.friction = 0f;
|
||||
// collider.enabled = false;
|
||||
// collider.enabled = true;
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// if (collider.sharedMaterial == null || collider.sharedMaterial.friction == 0f)
|
||||
// {
|
||||
// collider.sharedMaterial = new PhysicsMaterial2D();
|
||||
// collider.sharedMaterial.friction = 0.5f;
|
||||
// collider.enabled = false;
|
||||
// collider.enabled = true;
|
||||
// }
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
private void MovePlayer()
|
||||
{
|
||||
if (isMovementEnabled)
|
||||
{
|
||||
Jump();
|
||||
Run();
|
||||
FlipSprite();
|
||||
}
|
||||
|
||||
HandleAnimations();
|
||||
}
|
||||
|
||||
@@ -51,9 +78,6 @@ public class Player : MonoBehaviour
|
||||
|
||||
private void Run()
|
||||
{
|
||||
if (IsPlayerOnWall())
|
||||
return;
|
||||
|
||||
var actualRunSpeed = runSpeed;
|
||||
|
||||
// Speed Run Increase
|
||||
@@ -61,6 +85,10 @@ public class Player : MonoBehaviour
|
||||
{
|
||||
actualRunSpeed *= runFasterFactor;
|
||||
}
|
||||
if (IsPlayerOnWall())
|
||||
{
|
||||
actualRunSpeed = 0;
|
||||
}
|
||||
|
||||
// Run
|
||||
if (Input.GetKey(KeyCode.RightArrow))
|
||||
@@ -75,38 +103,45 @@ public class Player : MonoBehaviour
|
||||
|
||||
private bool IsPlayerOnGround()
|
||||
{
|
||||
return IsPointOnGround(transform.position + new Vector3(0.3f, 0, 0)) ||
|
||||
IsPointOnGround(transform.position + new Vector3(-0.3f, 0, 0));
|
||||
return IsPointOnGround(transform.position + new Vector3(collider.bounds.extents.x * 0.97f, 0, 0)) ||
|
||||
IsPointOnGround(transform.position) ||
|
||||
IsPointOnGround(transform.position - new Vector3(collider.bounds.extents.x, 0, 0));
|
||||
}
|
||||
|
||||
private bool IsPointOnGround(Vector2 position)
|
||||
{
|
||||
Debug.DrawLine(position, position - new Vector2(0, collider.bounds.extents.y * groundErrorThreshold));
|
||||
|
||||
RaycastHit2D hit = Physics2D.Raycast(position,
|
||||
-Vector2.up,
|
||||
groundErrorThreshold,
|
||||
collider.bounds.extents.y * groundErrorThreshold,
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
|
||||
return hit.collider != null && hit.collider.IsTouching(collider);
|
||||
}
|
||||
|
||||
private bool IsPlayerOnWall()
|
||||
private bool IsPlayerOnWall(bool ignoreGround = false)
|
||||
{
|
||||
return IsPointOnWall(transform.position + new Vector3(0, 1, 0)) ||
|
||||
IsPointOnWall(transform.position + new Vector3(0, -1, 0)) ||
|
||||
IsPointOnWall(transform.position + new Vector3(0, -2, 0));
|
||||
return IsPointOnWall(transform.position + new Vector3(0, collider.bounds.extents.y * 0.6f, 0), ignoreGround) ||
|
||||
IsPointOnWall(transform.position - new Vector3(0, collider.bounds.extents.y * 0.25f, 0), ignoreGround) ||
|
||||
IsPointOnWall(transform.position - new Vector3(0, collider.bounds.extents.y * 0.8f, 0), ignoreGround) ||
|
||||
IsPointOnWall(transform.position - new Vector3(0, collider.bounds.extents.y * 1.5f, 0), ignoreGround);
|
||||
}
|
||||
|
||||
private bool IsPointOnWall(Vector2 position)
|
||||
private bool IsPointOnWall(Vector2 position, bool ignoreGround = false)
|
||||
{
|
||||
Debug.DrawLine(position, position + new Vector2(collider.bounds.extents.x * wallErrorThreshold, 0), Color.green);
|
||||
Debug.DrawLine(position, position - new Vector2(collider.bounds.extents.x * wallErrorThreshold, 0), Color.green);
|
||||
|
||||
var rightHit = Physics2D.Linecast(position,
|
||||
position + wallLineCaseDistanceVector,
|
||||
position + new Vector2(collider.bounds.extents.x * wallErrorThreshold, 0),
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
var leftHit = Physics2D.Linecast(position,
|
||||
position - wallLineCaseDistanceVector,
|
||||
position - new Vector2(collider.bounds.extents.x * wallErrorThreshold, 0),
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
|
||||
return ((rightHit.collider != null && rightHit.collider.IsTouching(collider)) ||
|
||||
(leftHit.collider != null && leftHit.collider.IsTouching(collider))) && !IsPlayerOnGround();
|
||||
(leftHit.collider != null && leftHit.collider.IsTouching(collider))) && (!IsPlayerOnGround() || ignoreGround);
|
||||
}
|
||||
|
||||
private bool HasEncounteredEnemy()
|
||||
@@ -128,4 +163,14 @@ public class Player : MonoBehaviour
|
||||
animator.SetBool("IsGround", IsPlayerOnGround());
|
||||
animator.SetFloat("YVelocity", rigidBody.velocity.y);
|
||||
}
|
||||
|
||||
public void DisablePlayerMovement()
|
||||
{
|
||||
isMovementEnabled = false;
|
||||
}
|
||||
|
||||
public void EnablePlayerMovement()
|
||||
{
|
||||
isMovementEnabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user