Skip to content

Commit 14fd3a6

Browse files
committed
Fix target character hp not showing up
1 parent dbb9b4b commit 14fd3a6

File tree

1 file changed

+92
-5
lines changed

1 file changed

+92
-5
lines changed

Scripts/AimAtCursorPlayerCharacterController.cs

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class AimAtCursorPlayerCharacterController : BasePlayerCharacterControlle
88
{
99
[Header("Camera Controls Prefabs")]
1010
[SerializeField]
11-
private FollowCameraControls gameplayCameraPrefab;
11+
protected FollowCameraControls gameplayCameraPrefab;
1212
[SerializeField]
13-
private FollowCameraControls minimapCameraPrefab;
13+
protected FollowCameraControls minimapCameraPrefab;
1414

1515
[Header("Building Settings")]
1616
[SerializeField]
@@ -270,17 +270,99 @@ protected void UpdateFireInput()
270270

271271
protected void UpdateLookInput()
272272
{
273+
bool foundTargetEntity = false;
273274
bool isMobile = InputManager.useMobileInputOnNonMobile || Application.isMobilePlatform;
274275
Vector2 lookDirection;
275276
if (isMobile)
276277
{
277278
// Turn character by joystick
278279
lookDirection = new Vector2(InputManager.GetAxis("Mouse X", false), InputManager.GetAxis("Mouse Y", false));
280+
Transform tempTransform;
281+
IGameEntity tempGameEntity;
282+
Vector3 tempTargetPosition;
283+
int pickedCount;
284+
if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
285+
pickedCount = physicFunctions.Raycast(PlayerCharacterEntity.MeleeDamageTransform.position, lookDirection, 100f, Physics.DefaultRaycastLayers);
286+
else
287+
pickedCount = physicFunctions.Raycast(PlayerCharacterEntity.MeleeDamageTransform.position, new Vector3(lookDirection.x, 0, lookDirection.y), 100f, Physics.DefaultRaycastLayers);
288+
for (int i = 0; i < pickedCount; ++i)
289+
{
290+
tempTransform = physicFunctions.GetRaycastTransform(i);
291+
tempGameEntity = tempTransform.GetComponent<IGameEntity>();
292+
if (tempGameEntity != null)
293+
{
294+
foundTargetEntity = true;
295+
CacheUISceneGameplay.SetTargetEntity(tempGameEntity.Entity);
296+
PlayerCharacterEntity.SetTargetEntity(tempGameEntity.Entity);
297+
SelectedEntity = tempGameEntity.Entity;
298+
if (tempGameEntity.Entity != PlayerCharacterEntity.Entity)
299+
{
300+
if (tempGameEntity is IDamageableEntity)
301+
tempTargetPosition = (tempGameEntity as IDamageableEntity).OpponentAimTransform.position;
302+
else
303+
tempTargetPosition = tempGameEntity.GetTransform().position;
304+
if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
305+
lookDirection = (tempTargetPosition - CacheTransform.position).normalized;
306+
else
307+
lookDirection = (XZ(tempTargetPosition) - XZ(CacheTransform.position)).normalized;
308+
}
309+
break;
310+
}
311+
else
312+
{
313+
if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
314+
lookDirection = (physicFunctions.GetRaycastPoint(i) - CacheTransform.position).normalized;
315+
else
316+
lookDirection = (XZ(physicFunctions.GetRaycastPoint(i)) - XZ(CacheTransform.position)).normalized;
317+
}
318+
}
279319
}
280320
else
281321
{
282322
// Turn character follow cursor
283323
lookDirection = (InputManager.MousePosition() - new Vector3(Screen.width, Screen.height) * 0.5f).normalized;
324+
// Pick on object by mouse position
325+
Transform tempTransform;
326+
IGameEntity tempGameEntity;
327+
Vector3 tempTargetPosition;
328+
int pickedCount = physicFunctions.RaycastPickObjects(CacheGameplayCamera, InputManager.MousePosition(), Physics.DefaultRaycastLayers, 100f, out _);
329+
for (int i = 0; i < pickedCount; ++i)
330+
{
331+
tempTransform = physicFunctions.GetRaycastTransform(i);
332+
tempGameEntity = tempTransform.GetComponent<IGameEntity>();
333+
if (tempGameEntity != null)
334+
{
335+
foundTargetEntity = true;
336+
CacheUISceneGameplay.SetTargetEntity(tempGameEntity.Entity);
337+
PlayerCharacterEntity.SetTargetEntity(tempGameEntity.Entity);
338+
SelectedEntity = tempGameEntity.Entity;
339+
if (tempGameEntity.Entity != PlayerCharacterEntity.Entity)
340+
{
341+
if (tempGameEntity is IDamageableEntity)
342+
tempTargetPosition = (tempGameEntity as IDamageableEntity).OpponentAimTransform.position;
343+
else
344+
tempTargetPosition = tempGameEntity.GetTransform().position;
345+
if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
346+
lookDirection = (tempTargetPosition - CacheTransform.position).normalized;
347+
else
348+
lookDirection = (XZ(tempTargetPosition) - XZ(CacheTransform.position)).normalized;
349+
}
350+
break;
351+
}
352+
else
353+
{
354+
if (GameInstance.Singleton.DimensionType == DimensionType.Dimension2D)
355+
lookDirection = (physicFunctions.GetRaycastPoint(i) - CacheTransform.position).normalized;
356+
else
357+
lookDirection = (XZ(physicFunctions.GetRaycastPoint(i)) - XZ(CacheTransform.position)).normalized;
358+
}
359+
}
360+
}
361+
if (!foundTargetEntity)
362+
{
363+
CacheUISceneGameplay.SetTargetEntity(null);
364+
PlayerCharacterEntity.SetTargetEntity(null);
365+
SelectedEntity = null;
284366
}
285367

286368
// Turn character
@@ -491,7 +573,7 @@ public void FindAndSetBuildingAreaByMousePosition()
491573
/// <param name="count"></param>
492574
/// <param name="raycastPosition"></param>
493575
/// <returns></returns>
494-
private bool LoopSetBuildingArea(int count, Vector3 raycastPosition)
576+
protected bool LoopSetBuildingArea(int count, Vector3 raycastPosition)
495577
{
496578
IGameEntity gameEntity;
497579
BuildingArea buildingArea;
@@ -537,7 +619,7 @@ private bool LoopSetBuildingArea(int count, Vector3 raycastPosition)
537619
return false;
538620
}
539621

540-
private Vector3 GetBuildingPlacePosition(Vector3 position)
622+
protected Vector3 GetBuildingPlacePosition(Vector3 position)
541623
{
542624
if (CurrentGameInstance.DimensionType == DimensionType.Dimension3D)
543625
{
@@ -552,7 +634,7 @@ private Vector3 GetBuildingPlacePosition(Vector3 position)
552634
return position;
553635
}
554636

555-
private Quaternion GetBuildingPlaceRotation(float angles)
637+
protected Quaternion GetBuildingPlaceRotation(float angles)
556638
{
557639
Vector3 eulerAngles = Vector3.zero;
558640
if (CurrentGameInstance.DimensionType == DimensionType.Dimension3D)
@@ -570,5 +652,10 @@ private Quaternion GetBuildingPlaceRotation(float angles)
570652
}
571653
return Quaternion.Euler(eulerAngles);
572654
}
655+
656+
protected Vector2 XZ(Vector3 vector3)
657+
{
658+
return new Vector2(vector3.x, vector3.z);
659+
}
573660
}
574661
}

0 commit comments

Comments
 (0)