fix collision system, ladders, and enemy AI

This commit is contained in:
Vova
2023-07-21 14:55:39 +03:00
parent 5849bcd270
commit 83c44a51a5
8 changed files with 1338 additions and 278 deletions
+178
View File
@@ -0,0 +1,178 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &6742600071965714980
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7515349249133635430}
- component: {fileID: 1557174451233602412}
m_Layer: 3
m_Name: Visual
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &7515349249133635430
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6742600071965714980}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 7345322822309755345}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!212 &1557174451233602412
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6742600071965714980}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 0
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2}
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: -133646357
m_SortingLayer: 2
m_SortingOrder: 0
m_Sprite: {fileID: -974747892, guid: 7c2d6acd22d2d0c40b84d99967afb42a, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 1.0230769, y: 1.0076923}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!1 &8175732239415438715
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 7345322822309755345}
- component: {fileID: 3364452304498334467}
- component: {fileID: 1500185933721369279}
m_Layer: 3
m_Name: Ladder
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &7345322822309755345
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8175732239415438715}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 7515349249133635430}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!61 &3364452304498334467
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8175732239415438715}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_ForceSendLayers:
serializedVersion: 2
m_Bits: 4294967295
m_ForceReceiveLayers:
serializedVersion: 2
m_Bits: 4294967295
m_ContactCaptureLayers:
serializedVersion: 2
m_Bits: 4294967295
m_CallbackLayers:
serializedVersion: 2
m_Bits: 4294967295
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 0.85, y: 1}
m_EdgeRadius: 0
--- !u!114 &1500185933721369279
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8175732239415438715}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: edf40d39eadb17a4ebb0368b3ac5399a, type: 3}
m_Name:
m_EditorClassIdentifier:
_elementSO: {fileID: 11400000, guid: 8a480e639bc6f0c44815c3b3dd16e0aa, type: 2}
+7
View File
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b38f313f4560587478b6bc53fc9aaf5c
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+1060 -177
View File
File diff suppressed because it is too large Load Diff
+39 -86
View File
@@ -1,21 +1,18 @@
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Tilemaps;
public abstract class Character : MonoBehaviour
{
[SerializeField]
protected Animator _animator;
[SerializeField]
private float MovementSpeed = 1.5f;
private float _movementSpeed = 1.5f;
[SerializeField]
protected GameObject _bonesSide;
[SerializeField]
protected GameObject _bonesBack;
[SerializeField]
protected GameObject _spawnPoint;
[SerializeField]
protected Tilemap _tileMap;
[SerializeField]
private LayerMask _mapLayer;
@@ -40,7 +37,7 @@ public abstract class Character : MonoBehaviour
_body = GetComponent<Rigidbody2D>();
_capsuleCollider = GetComponent<CapsuleCollider2D>();
_cellSize = new Vector2(_tileMap.cellSize.x, _tileMap.cellSize.y);
_cellSize = new Vector2(0.6f, 1f);
}
protected void Spawn()
@@ -50,25 +47,17 @@ public abstract class Character : MonoBehaviour
protected void MoveTo(float inputHorizontal, float inputVertical)
{
var downBlock = GetMapElement(Vector2.down);
var block = GetMapElement();
if (downBlock == MapElementType.Water)
if (block?.ElementSO.ElementType == MapElementType.Water)
{
Death();
}
_isOnBridge = downBlock == MapElementType.Bridge && !_isFalling;
GetMapElement(Vector2.up);
if (downBlock == MapElementType.Wall || _isOnLadder || _isOnBridge || downBlock == MapElementType.BreakableWall)
_isOnBridge = block?.ElementSO.ElementType == MapElementType.Bridge && !_isFalling;
if (block)
{
var leftCheck = GetMapElement(Vector2.left);
var rightCheck = GetMapElement(Vector2.right);
//print($"LeftCheck:{leftCheck} right:{rightCheck}");
isAllowLeft = !(leftCheck == MapElementType.Wall || leftCheck == MapElementType.BreakableWall);
isAllowRight = !(rightCheck == MapElementType.Wall || rightCheck == MapElementType.BreakableWall);
isAllowVertical = false;
_isFalling = false;
float h_movement = inputHorizontal;
@@ -79,26 +68,30 @@ public abstract class Character : MonoBehaviour
}
SetWalkingAnimation(h_movement != 0);
;
_isOnLadder = GetMapElement(Vector2.zero) == MapElementType.Ladder || downBlock == MapElementType.Ladder;
_isOnLadder = block.ElementSO.ElementType == MapElementType.Ladder;
if (_isOnLadder)
{
float ladderCenterDistance = Mathf.Abs(transform.position.x - block.transform.position.x);
float v_movement = inputVertical;
isAllowVertical = true;
isAllowVertical = ladderCenterDistance < 0.3f;
SetClimbingAnimation(v_movement != 0);
_body.velocity = new Vector2(h_movement * MovementSpeed, v_movement * MovementSpeed);
_body.velocity = new Vector2(h_movement * _movementSpeed, v_movement * _movementSpeed);
}
else
{
_body.velocity = new Vector2(h_movement * MovementSpeed, _body.velocity.y);
_body.velocity = new Vector2(h_movement * _movementSpeed, _body.velocity.y);
}
}
else
{
_isFalling = true;
_isOnLadder = false;
}
@@ -115,7 +108,7 @@ public abstract class Character : MonoBehaviour
_body.velocity = new Vector2(0, _body.velocity.y);
SetWalkingAnimation(false);
if (downBlock == MapElementType.Ladder)
if (block?.ElementSO.ElementType == MapElementType.Ladder)
{
_body.velocity = Vector2.zero;
_isOnLadder = true;
@@ -131,69 +124,27 @@ public abstract class Character : MonoBehaviour
}
private MapElementType GetMapElement(Vector3 direction)
private MapElement GetMapElement()
{
var bounds = _capsuleCollider.bounds.min;
//var cell = _tileMap.WorldToCell(bounds + direction);
//Vector2 cell2d = new Vector2(cell.x + 0.5f, cell.y + 0.3f);
Vector2 cellSize = _cellSize;
var playerBounds = _capsuleCollider.bounds.center;
//DrawBounds(playerBounds, cellSize, Color.cyan);
playerBounds = playerBounds + direction;
var raycastHit = BoxCast(playerBounds, cellSize, 0f, Vector3.forward, .01f, _mapLayer);
MapElementType returnValue;
var collider = BoxCast(playerBounds, _cellSize, 0f, Vector3.forward, .01f, _mapLayer);
Color color = Color.red;
if (raycastHit)
MapElement mapElement = null;
if (collider.Length > 0)
{
var mapElement = raycastHit.transform.GetComponent<MapElement>();
if (!mapElement.IsEnabled)
var elements=collider.Select(x=>x.transform.GetComponent<MapElement>());
mapElement = elements.Where(x => x.ElementSO.ElementType == MapElementType.Ladder).FirstOrDefault();
if(mapElement == null)
{
color = Color.white;
returnValue = MapElementType.Empty;
}
else
{
returnValue = mapElement.ElementSO.ElementType;
switch (returnValue)
{
case MapElementType.Wall:
color = Color.green;
break;
case MapElementType.Ladder:
color = Color.yellow;
break;
case MapElementType.BreakableWall:
color = Color.magenta;
break;
}
mapElement = elements.First();
}
}
else
{
returnValue = MapElementType.Empty;
}
// DrawBounds(playerBounds, cellSize, color);
return returnValue;
return mapElement;
}
//private static void DrawBounds(Vector2 cell2d, Vector2 cellSize, Color color)
//{
// Debug.DrawLine(cell2d, cell2d + new Vector2(cellSize.x, 0), color);
// Debug.DrawLine(cell2d + new Vector2(cellSize.x, 0), cell2d + new Vector2(cellSize.x, cellSize.y), color);
// Debug.DrawLine(cell2d, cell2d + new Vector2(0, cellSize.y), color);
// Debug.DrawLine(cell2d + new Vector2(0, cellSize.y), cell2d + new Vector2(cellSize.x, cellSize.y), color);
//}
protected abstract void OnDeath();
protected abstract void SetWalkingAnimation(bool isWalking);
protected abstract void SetClimbingAnimation(bool isClimbing);
@@ -209,10 +160,10 @@ public abstract class Character : MonoBehaviour
_facingRight = !_facingRight;
}
static public RaycastHit2D BoxCast(Vector2 origin, Vector2 size, float angle, Vector2 direction, float distance, int mask)
static public Collider2D[] BoxCast(Vector2 origin, Vector2 size, float angle, Vector2 direction, float distance, int mask)
{
RaycastHit2D hit = Physics2D.BoxCast(origin, size, angle, direction, distance, mask);
//RaycastHit2D hit = Physics2D.BoxCast(origin, size, angle, direction, distance, mask);
var collider = Physics2D.OverlapBoxAll(origin, size, angle, mask);
//Setting up the points to draw the cast
Vector2 p1, p2, p3, p4, p5, p6, p7, p8;
float w = size.x * 0.5f;
@@ -241,7 +192,7 @@ public abstract class Character : MonoBehaviour
//Drawing the cast
Color castColor = hit ? Color.red : Color.green;
Color castColor = collider.Length > 0 ? Color.red : Color.green;
Debug.DrawLine(p1, p2, castColor);
Debug.DrawLine(p2, p3, castColor);
Debug.DrawLine(p3, p4, castColor);
@@ -256,12 +207,14 @@ public abstract class Character : MonoBehaviour
Debug.DrawLine(p2, p6, Color.grey);
Debug.DrawLine(p3, p7, Color.grey);
Debug.DrawLine(p4, p8, Color.grey);
if (hit)
{
var color = hit.point.x > origin.x ? Color.yellow : Color.cyan;
Debug.DrawLine(hit.point, hit.point + hit.normal.normalized * 0.2f, color);
}
return hit;
//collider
//if (hit)
//{
// var color = hit.point.x > origin.x ? Color.yellow : Color.cyan;
// Debug.DrawLine(hit.point, hit.point + hit.normal.normalized * 0.2f, color);
//}
return collider;
}
}
+46 -9
View File
@@ -1,4 +1,4 @@
using UnityEngine;
using UnityEngine;
public class EnemyAI : Character
{
@@ -18,22 +18,59 @@ public class EnemyAI : Character
{
float horizontal = 0;
float vertical = 0;
if (Player.Instance.transform.position.x - transform.position.x < -0.05f && isAllowLeft &&!_isOnLadder)
Vector2 directionToPlayer = Player.Instance.transform.position - transform.position;
directionToPlayer.Normalize(); // Нормализуем вектор, чтобы скорость была постоянной
// Преследуем игрока только по горизонтали, если враг не на лестнице
if (!_isOnLadder)
{
horizontal = -1;
horizontal = directionToPlayer.x;
}
else if (Player.Instance.transform.position.x - transform.position.x > 0.05f && isAllowRight && !_isOnLadder)
else
{
horizontal = 1;
// Вычисляем направление движения к игроку по вертикали, если враг на лестнице
float verticalDistance = Player.Instance.transform.position.y - transform.position.y;
float verticalDirection = Mathf.Sign(verticalDistance);
vertical = verticalDirection;
}
else if (Mathf.Abs(Player.Instance.transform.position.y - transform.position.y) > 0.05f && isAllowVertical)
if (!isAllowVertical)
{
var deltaY = Player.Instance.transform.position.y - transform.position.y;
vertical = deltaY > 0 ? 1 : -1;
float verticalDistance = Player.Instance.transform.position.y - transform.position.y;
float verticalDirection = Mathf.Sign(verticalDistance);
vertical = verticalDirection;
}
float horizontalDistance = Player.Instance.transform.position.x - transform.position.x;
float horizontalDirection = Mathf.Sign(horizontalDistance);
horizontal = horizontalDirection;
//if (Player.Instance.transform.position.x - transform.position.x < -0.05f && isAllowLeft &&!_isOnLadder)
//{
// horizontal = -1;
//}
//else if (Player.Instance.transform.position.x - transform.position.x > 0.05f && isAllowRight && !_isOnLadder)
//{
// horizontal = 1;
//}
//else if (Mathf.Abs(Player.Instance.transform.position.y - transform.position.y) > 0.05f && isAllowVertical)
//{
// var deltaY = Player.Instance.transform.position.y - transform.position.y;
// vertical = deltaY > 0 ? 1 : -1;
//}
if (Input.GetKey(KeyCode.F))
{ horizontal = -1; }
if (Input.GetKey(KeyCode.H))
{ horizontal = 1; }
if (Input.GetKey(KeyCode.T))
{ vertical = 1; }
if (Input.GetKey(KeyCode.G))
{ vertical = -1; }
base.MoveTo(horizontal, vertical);
}
}
+2
View File
@@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.UIElements;
public class MapElement : MonoBehaviour, IMapElement
{
@@ -7,6 +8,7 @@ public class MapElement : MonoBehaviour, IMapElement
public MapElementSO ElementSO => _elementSO;
public bool IsEnabled { get; set; } = true;
public Transform GlobalTransform { get; set; }
public virtual void Hit()
{
+1 -1
View File
@@ -89,7 +89,7 @@ public class Player : Character
_animator.SetTrigger("Body_ThrowHammer");
}
}
base.MoveTo(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
base.MoveTo(Input.GetAxisRaw("Horizontal"), isAllowVertical? Input.GetAxisRaw("Vertical"):0);
}
public void ThrowHammerObject()
+5 -5
View File
@@ -69,7 +69,7 @@ TextureImporter:
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
maxTextureSize: 512
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
@@ -143,10 +143,10 @@ TextureImporter:
border: {x: 0, y: 0, z: 0, w: 0}
outline: []
physicsShape:
- - {x: -53, y: 66}
- {x: -52, y: -66}
- {x: 58, y: -66}
- {x: 55, y: 66}
- - {x: -49, y: 66}
- {x: -48, y: -66}
- {x: 56, y: -66}
- {x: 54, y: 66}
tessellationDetail: 0
bones: []
spriteID: ee075fd35e03f744bae3d980048a22c5