...
This commit is contained in:
@ -86,12 +86,8 @@ namespace Animators.Leonid_Animator
|
||||
private void HandleRotation(float delta)
|
||||
{
|
||||
if (Mathf.Abs(_inputHandler.horizontal) + Mathf.Abs(_inputHandler.vertical) < 0.1)
|
||||
{
|
||||
print("stop");
|
||||
return;
|
||||
}
|
||||
|
||||
print("begin");
|
||||
var moveAmount = _inputHandler.moveAmount;
|
||||
var targetDir = _cameraObject.forward * _inputHandler.vertical
|
||||
+ _cameraObject.right * _inputHandler.horizontal;
|
||||
|
@ -15,9 +15,9 @@ namespace CameraScripts
|
||||
|
||||
public static CameraHandler Singleton;
|
||||
|
||||
public const float LookSpeed = 0.1f;
|
||||
public const float FollowSpeed = 0.1f;
|
||||
public const float PivotSpeed = 0.03f;
|
||||
[SerializeField] private float LookSpeed = 0.1f;
|
||||
[SerializeField] private float FollowSpeed = 0.1f;
|
||||
[SerializeField] private float PivotSpeed = 0.03f;
|
||||
|
||||
private float _defaultPosition;
|
||||
private float _lookAngle;
|
||||
@ -50,12 +50,7 @@ namespace CameraScripts
|
||||
var rotation = Vector3.zero;
|
||||
rotation.y = _lookAngle;
|
||||
var targetRotation = Quaternion.Euler(rotation);
|
||||
_myTransform.rotation = targetRotation;
|
||||
|
||||
rotation = Vector3.zero;
|
||||
rotation.x = _pivotAngle;
|
||||
targetRotation = Quaternion.Euler(rotation);
|
||||
cameraPivotTransform.localRotation = targetRotation;
|
||||
targetTransform.rotation = targetRotation;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
public interface ICharacter
|
||||
using System;
|
||||
|
||||
public interface ICharacter
|
||||
{
|
||||
Character GetCharacter { get; }
|
||||
void ResetCharacter();
|
||||
event Action<bool> OnDeathEvent;
|
||||
}
|
@ -16,6 +16,8 @@ public class MovementController : MonoBehaviour
|
||||
[SerializeField] private GameObject flag;
|
||||
public float DistanceToGo { get; private set; }
|
||||
public float RemainingDistance => navMeshAgent.remainingDistance;
|
||||
public Vector3 Velocity => navMeshAgent.velocity;
|
||||
|
||||
private Dictionary<int, NavPoint> _idNavPointDict;
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Animators.Leonid_Animator;
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.Actuators;
|
||||
using Unity.MLAgents.Sensors;
|
||||
@ -10,51 +10,57 @@ using UnityEngine;
|
||||
public class NPC : Agent, ICharacter
|
||||
{
|
||||
[HideInInspector]
|
||||
private Character AgentCharacter;
|
||||
public CharacterCondition Condition;
|
||||
private FlagZone flagZone = null;
|
||||
private Character _agentCharacter;
|
||||
|
||||
public INpcBaseState NpcState { get; private set; }
|
||||
private CharacterCondition _condition;
|
||||
private FlagZone _flagZone = null;
|
||||
|
||||
private INpcBaseState NpcState { get; set; }
|
||||
public INpcBaseBodyState NpcBodyState { get; private set; }
|
||||
|
||||
public Character GetCharacter => AgentCharacter;
|
||||
public Character GetCharacter => _agentCharacter;
|
||||
|
||||
private NpcDirectPointState DirectState;
|
||||
private NpcInCoverState CoverState;
|
||||
private NpcRunningState RunningState;
|
||||
private NpcDirectPointState _directState;
|
||||
private NpcInCoverState _coverState;
|
||||
private NpcRunningState _runningState;
|
||||
|
||||
private NpcStandingState StandingState;
|
||||
private NpcCrouchingState CrouchingState;
|
||||
private NpcStandingState _standingState;
|
||||
private NpcCrouchingState _crouchingState;
|
||||
|
||||
private MovementController moveController;
|
||||
private BufferSensorComponent bufferSensor;
|
||||
private MovementController _moveController;
|
||||
private BufferSensorComponent _bufferSensor;
|
||||
private AnimatorHandler _animatorHandler;
|
||||
private AimAssistant _assistant;
|
||||
|
||||
private Dictionary<int, NavPoint> _navPointIdDict;
|
||||
|
||||
private Dictionary<int, NavPoint> navPointIdDict;
|
||||
|
||||
#region UnityEvents and ML
|
||||
private void Awake()
|
||||
{
|
||||
DirectState = new NpcDirectPointState();
|
||||
CoverState = new NpcInCoverState();
|
||||
RunningState = new NpcRunningState();
|
||||
NpcState = DirectState;
|
||||
_directState = new NpcDirectPointState();
|
||||
_coverState = new NpcInCoverState();
|
||||
_runningState = new NpcRunningState();
|
||||
NpcState = _directState;
|
||||
|
||||
CrouchingState = new NpcCrouchingState();
|
||||
StandingState = new NpcStandingState();
|
||||
NpcBodyState = StandingState;
|
||||
_crouchingState = new NpcCrouchingState();
|
||||
_standingState = new NpcStandingState();
|
||||
NpcBodyState = _standingState;
|
||||
|
||||
AgentCharacter = new Character();
|
||||
Condition = AgentCharacter.Condition;
|
||||
_agentCharacter = new Character();
|
||||
_condition = _agentCharacter.Condition;
|
||||
|
||||
moveController = gameObject.GetComponent<MovementController>();
|
||||
bufferSensor = gameObject.GetComponent<BufferSensorComponent>();
|
||||
_moveController = gameObject.GetComponent<MovementController>();
|
||||
_bufferSensor = gameObject.GetComponent<BufferSensorComponent>();
|
||||
_animatorHandler = gameObject.GetComponent<AnimatorHandler>();
|
||||
_assistant = gameObject.GetComponent<AimAssistant>();
|
||||
|
||||
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
if (flagZone is null)
|
||||
_flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
if (_flagZone is null)
|
||||
Debug.LogError("Flag Is Not Set");
|
||||
|
||||
navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (navPointIdDict is null)
|
||||
_navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (_navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
}
|
||||
|
||||
@ -65,54 +71,54 @@ public class NPC : Agent, ICharacter
|
||||
|
||||
public override void OnEpisodeBegin()
|
||||
{
|
||||
if (navPointIdDict is null)
|
||||
if (_navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
|
||||
NpcState = DirectState;
|
||||
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
NpcState = _directState;
|
||||
_flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
}
|
||||
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
// Debug.Log("Collect observations called!");
|
||||
navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (navPointIdDict is null)
|
||||
_navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (_navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
var candidates = moveController.GetPointsCandidate();
|
||||
var candidates = _moveController.GetPointsCandidate();
|
||||
|
||||
//common sensors
|
||||
sensor.AddObservation(GameManager.IsHaveSeenByEnemy(AgentCharacter.Team.GetOppositeTeam(),
|
||||
sensor.AddObservation(GameManager.IsHaveSeenByEnemy(_agentCharacter.Team.GetOppositeTeam(),
|
||||
NpcBodyState.GetPointToHit(gameObject)).ToInt());
|
||||
sensor.AddObservation(AgentCharacter.LastTimeHit);
|
||||
sensor.AddObservation((!flagZone.IsNotOccup).ToInt());
|
||||
sensor.AddObservation(Condition.GetHealthPointsInQuantile());
|
||||
sensor.AddObservation(Condition.GetArmourPointsInQuantile());
|
||||
sensor.AddObservation(_agentCharacter.LastTimeHit);
|
||||
sensor.AddObservation((!_flagZone.IsNotOccup).ToInt());
|
||||
sensor.AddObservation(_condition.GetHealthPointsInQuantile());
|
||||
sensor.AddObservation(_condition.GetArmourPointsInQuantile());
|
||||
sensor.AddObservation(candidates.Count);
|
||||
sensor.AddObservation(moveController.PointStartID);
|
||||
sensor.AddObservation(moveController.PointEndID);
|
||||
sensor.AddObservation(_moveController.PointStartID);
|
||||
sensor.AddObservation(_moveController.PointEndID);
|
||||
// Debug.Log("Done common!");
|
||||
//state sensors
|
||||
sensor.AddObservation((int)NpcState.State);
|
||||
sensor.AddObservation((int)NpcBodyState.State);
|
||||
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, AgentCharacter.Team));
|
||||
sensor.AddObservation(navPointIdDict[moveController.PointStartID].DeathAttr);
|
||||
sensor.AddObservation(navPointIdDict[moveController.PointEndID].DeathAttr);
|
||||
sensor.AddObservation(moveController.FlagDistance);
|
||||
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, _agentCharacter.Team));
|
||||
sensor.AddObservation(_navPointIdDict[_moveController.PointStartID].DeathAttr);
|
||||
sensor.AddObservation(_navPointIdDict[_moveController.PointEndID].DeathAttr);
|
||||
sensor.AddObservation(_moveController.FlagDistance);
|
||||
// Debug.Log("Done state sensors!");
|
||||
|
||||
//point sensors
|
||||
foreach (var point in candidates)
|
||||
{
|
||||
var position = transform.position;
|
||||
bufferSensor.AppendObservation(new float[] {
|
||||
_bufferSensor.AppendObservation(new float[] {
|
||||
point.DeathAttr,
|
||||
(int)point.navType,
|
||||
//4 flagEnemyDistance
|
||||
GameManager.IsCloserToFlagFromNextNavPoint(point, position).ToInt(),
|
||||
//5 EnemyVsNavPointDistance
|
||||
GameManager.IsCloserToEnemyThanToNextNavPoint(point, position, AgentCharacter.Team.GetOppositeTeam()).ToInt(),
|
||||
GameManager.IsCloserToEnemyThanToNextNavPoint(point, position, _agentCharacter.Team.GetOppositeTeam()).ToInt(),
|
||||
//6 Have been seen by enemy in this point
|
||||
GameManager.IsHaveSeenByEnemy(AgentCharacter.Team.GetOppositeTeam(),
|
||||
GameManager.IsHaveSeenByEnemy(_agentCharacter.Team.GetOppositeTeam(),
|
||||
point.Position).ToInt()
|
||||
});
|
||||
}
|
||||
@ -126,16 +132,16 @@ public class NPC : Agent, ICharacter
|
||||
// Debug.Log(result[0] + " " + result[1]);
|
||||
if (result[0] == 0)
|
||||
{
|
||||
if (navPointIdDict[moveController.PointStartID].navType != NavPointType.Cover)
|
||||
if (_navPointIdDict[_moveController.PointStartID].navType != NavPointType.Cover)
|
||||
return;
|
||||
NpcState = CoverState;
|
||||
NpcState = _coverState;
|
||||
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: Peek(); break;
|
||||
case 1: Cover(); break;
|
||||
case 3: Peek(); moveController.GoToNextNavPoint(navPointIdDict[result[2]]); break;
|
||||
case 4: NpcState = DirectState; break;
|
||||
case 3: Peek(); _moveController.GoToNextNavPoint(_navPointIdDict[result[2]]); break;
|
||||
case 4: NpcState = _directState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
@ -143,7 +149,7 @@ public class NPC : Agent, ICharacter
|
||||
if (result[0] == 1)
|
||||
{
|
||||
// Debug.Log("BEFORE SOme shitty if >:(");
|
||||
if (navPointIdDict[moveController.PointStartID].navType != NavPointType.Direction)
|
||||
if (_navPointIdDict[_moveController.PointStartID].navType != NavPointType.Direction)
|
||||
{
|
||||
// Debug.Log("SOme shitty if >:(");
|
||||
return;
|
||||
@ -152,64 +158,72 @@ public class NPC : Agent, ICharacter
|
||||
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: moveController.GoToNextNavPoint(navPointIdDict[result[2]]);
|
||||
NpcState = RunningState; Debug.Log("Go to point " + result[2]);break;
|
||||
case 1: NpcState = DirectState; break;
|
||||
case 0: _moveController.GoToNextNavPoint(_navPointIdDict[result[2]]);
|
||||
NpcState = _runningState; Debug.Log("Go to point " + result[2]);break;
|
||||
case 1: NpcState = _directState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
|
||||
if (result[0] == 2)
|
||||
{
|
||||
if (moveController.PointStartID == moveController.PointEndID && moveController.PointEndID != -1)
|
||||
if (_moveController.PointStartID == _moveController.PointEndID && _moveController.PointEndID != -1)
|
||||
return;
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: moveController.StopOnPath(); NpcState = DirectState; break;
|
||||
case 1: moveController.ReturnToStartPoint(); NpcState = RunningState; break;
|
||||
case 0: _moveController.StopOnPath(); NpcState = _directState; break;
|
||||
case 1: _moveController.ReturnToStartPoint(); NpcState = _runningState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
// Debug.Log("Actions processed!");
|
||||
}
|
||||
|
||||
private void UpdateAnimatorValues()
|
||||
{
|
||||
var movementDir = _moveController.Velocity;
|
||||
//Тут может быть косяк, так как я не помню горизонтальное по x или y.
|
||||
_animatorHandler.UpdateAnimatorValues(movementDir.x, movementDir.y,
|
||||
false, NpcBodyState == _crouchingState, _assistant._isFiring);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public event Action<NpcBodyState> OnChangePosition;
|
||||
private void Peek()
|
||||
{
|
||||
OnChangePosition?.Invoke(global::NpcBodyState.Standing);
|
||||
NpcBodyState = StandingState;
|
||||
NpcBodyState = _standingState;
|
||||
}
|
||||
|
||||
private void Cover()
|
||||
{
|
||||
OnChangePosition?.Invoke(global::NpcBodyState.Crouching);
|
||||
NpcBodyState = CrouchingState;
|
||||
NpcBodyState = _crouchingState;
|
||||
}
|
||||
|
||||
public event Action<bool> OnDeathEvent;
|
||||
public event Action<int, Team> OnDamageRecieved;
|
||||
public void GetDamage(int damage)
|
||||
{
|
||||
AgentCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
|
||||
Condition.GiveHealth(-Mathf.RoundToInt(damage * (1 - Condition.ArmourPoints * 0.5f)));
|
||||
Condition.GiveArmour(-Mathf.RoundToInt(Mathf.Sqrt(damage) * 5));
|
||||
OnDamageRecieved?.Invoke(damage, AgentCharacter.Team);
|
||||
_agentCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
|
||||
_condition.GiveHealth(-Mathf.RoundToInt(damage * (1 - _condition.ArmourPoints * 0.5f)));
|
||||
_condition.GiveArmour(-Mathf.RoundToInt(Mathf.Sqrt(damage) * 5));
|
||||
OnDamageRecieved?.Invoke(damage, _agentCharacter.Team);
|
||||
|
||||
if (Condition.HealthPoints < 0)
|
||||
if (_condition.HealthPoints < 0)
|
||||
{
|
||||
MapManager.AddDeathAttributeToPoints(moveController.PointStartID, moveController.PointEndID,
|
||||
moveController.DistanceToGo, moveController.RemainingDistance);
|
||||
OnDeathEvent?.Invoke(true);
|
||||
MapManager.AddDeathAttributeToPoints(_moveController.PointStartID, _moveController.PointEndID,
|
||||
_moveController.DistanceToGo, _moveController.RemainingDistance);
|
||||
var pos = gameObject.transform.position;
|
||||
var id = moveController.PointStartID;
|
||||
var id = _moveController.PointStartID;
|
||||
CharacterFactory.Instance.ReSpawn(this, ref pos, ref id);
|
||||
}
|
||||
}
|
||||
|
||||
//public event OnDeathEvent<
|
||||
|
||||
public void ResetCharacter()
|
||||
{
|
||||
Condition.Reset();
|
||||
_condition.Reset();
|
||||
EndEpisode();
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ public class Player : MonoBehaviour, ICharacter
|
||||
Debug.LogWarning("Pooled object was destroyed");
|
||||
}
|
||||
|
||||
public event Action<object> OnKilledEvent;
|
||||
public event Action<bool> OnDeathEvent;
|
||||
public void GetDamage(float damage)
|
||||
{
|
||||
PlayerCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
|
||||
@ -28,7 +28,7 @@ public class Player : MonoBehaviour, ICharacter
|
||||
Condition.GiveArmour(-Mathf.RoundToInt(Mathf.Sqrt(damage) * 5));
|
||||
|
||||
if (Condition.HealthPoints < 0)
|
||||
OnKilledEvent?.Invoke(this);
|
||||
OnDeathEvent?.Invoke(true);
|
||||
}
|
||||
|
||||
public void ResetCharacter()
|
||||
|
@ -1,19 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.MLAgents;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
private static GameManager instance;
|
||||
public static GameManager Instance => instance;
|
||||
private static GameManager _instance;
|
||||
public static GameManager Instance => _instance;
|
||||
|
||||
private static SimpleMultiAgentGroup defendersTeam = new SimpleMultiAgentGroup();
|
||||
private static SimpleMultiAgentGroup attackersTeam = new SimpleMultiAgentGroup();
|
||||
private static SimpleMultiAgentGroup _defendersTeam = new SimpleMultiAgentGroup();
|
||||
private static SimpleMultiAgentGroup _attackersTeam = new SimpleMultiAgentGroup();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (instance is null)
|
||||
instance = this;
|
||||
if (_instance is null)
|
||||
_instance = this;
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
@ -32,23 +33,23 @@ public class GameManager : MonoBehaviour
|
||||
{
|
||||
var agent = item as NPC;
|
||||
if (agent.GetCharacter.Team == Team.Attackers)
|
||||
attackersTeam.RegisterAgent(item);
|
||||
_attackersTeam.RegisterAgent(item);
|
||||
else
|
||||
defendersTeam.RegisterAgent(item);
|
||||
_defendersTeam.RegisterAgent(item);
|
||||
}
|
||||
}
|
||||
|
||||
private static SimpleMultiAgentGroup getAgentList(Team team)
|
||||
private static SimpleMultiAgentGroup GetAgentList(Team team)
|
||||
{
|
||||
if (team == Team.Attackers)
|
||||
return attackersTeam;
|
||||
return _attackersTeam;
|
||||
else
|
||||
return defendersTeam;
|
||||
return _defendersTeam;
|
||||
}
|
||||
|
||||
public static bool IsCloserToEnemyThanToNextNavPoint(NavPoint navPoint, Vector3 currentTransform, Team oppositeTeam)
|
||||
{
|
||||
var agentGroup = getAgentList(oppositeTeam);
|
||||
var agentGroup = GetAgentList(oppositeTeam);
|
||||
|
||||
var distToNavPoint = (currentTransform - navPoint.Position).magnitude;
|
||||
foreach (var agent in agentGroup.GetRegisteredAgents())
|
||||
@ -65,7 +66,7 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
public static bool IsEnemyNearby(Vector3 currentTransform, Team oppositeTeam)
|
||||
{
|
||||
var agentGroup = getAgentList(oppositeTeam);
|
||||
var agentGroup = GetAgentList(oppositeTeam);
|
||||
|
||||
foreach (var agent in agentGroup.GetRegisteredAgents())
|
||||
if ((currentTransform - agent.transform.position).magnitude < SettingsReader.Instance.GetSettings.ViewDistance)
|
||||
@ -84,18 +85,23 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
public static bool IsHaveSeenByEnemy(Team oppositeTeam, Vector3 position)
|
||||
{
|
||||
var agentGroup = getAgentList(oppositeTeam);
|
||||
return GetVisibleEnemies(oppositeTeam, position).Count > 0 ? true : false;
|
||||
}
|
||||
|
||||
public static List<GameObject> GetVisibleEnemies(Team oppositeTeam, Vector3 position)
|
||||
{
|
||||
var agentGroup = GetAgentList(oppositeTeam);
|
||||
RaycastHit rayHit = new RaycastHit();
|
||||
foreach (var agent in agentGroup.GetRegisteredAgents() )
|
||||
{
|
||||
var npc = agent as NPC;
|
||||
if (Physics.Raycast(position,
|
||||
(npc.NpcBodyState.GetPointToHit(npc.gameObject) - position).normalized,
|
||||
out rayHit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
(npc.NpcBodyState.GetPointToHit(npc.gameObject) - position).normalized,
|
||||
out rayHit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
if (rayHit.collider.gameObject.GetComponent<ICharacter>() != null)
|
||||
return true;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if ((SettingsReader.Instance.GetSettings.HasHumanAttacker == true && oppositeTeam == Team.Attackers) ||
|
||||
@ -103,15 +109,15 @@ public class GameManager : MonoBehaviour
|
||||
{
|
||||
var player = CharacterFactory.Instance.player;
|
||||
if (Physics.Raycast(position,
|
||||
(player.GetComponent<MeshRenderer>().bounds.center - position).normalized,
|
||||
out rayHit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
(player.GetComponent<MeshRenderer>().bounds.center - position).normalized,
|
||||
out rayHit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
if (rayHit.collider.gameObject.GetComponent<ICharacter>() != null)
|
||||
return true;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
private void FlagCaptured(Team team)
|
||||
|
62
Assets/Scripts/Weapons/AimAssistant.cs
Normal file
62
Assets/Scripts/Weapons/AimAssistant.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(NPC))]
|
||||
public class AimAssistant : MonoBehaviour
|
||||
{
|
||||
public GameObject enemy;
|
||||
public bool _isFiring = false;
|
||||
private ICharacter _myNpc;
|
||||
public float lookSpeed = 200f;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_myNpc = GetComponent<NPC>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
//Ищем противника на сцене.
|
||||
if (_isFiring == false)
|
||||
{
|
||||
var enemies = GameManager.GetVisibleEnemies(_myNpc.GetCharacter.Team, transform.position);
|
||||
enemy = enemies[new System.Random().Next(enemies.Count)];
|
||||
if (enemies.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
var character = enemy.GetComponent<ICharacter>();
|
||||
character.OnDeathEvent += _ => _isFiring = false;
|
||||
_isFiring = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Raycast до противника enemy
|
||||
var range = 200f;
|
||||
while (true)
|
||||
{
|
||||
if (!Physics.Raycast(this.transform.position, this.transform.forward, out var hit, range))
|
||||
{
|
||||
_isFiring = false;
|
||||
enemy = null;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetComponent<Shooting>().Shoot();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var direction = enemy.transform.position - gameObject.transform.position;
|
||||
var targetRotation = Quaternion.LookRotation(direction);
|
||||
var lookAt = Quaternion.RotateTowards(gameObject.transform.rotation, targetRotation,
|
||||
Time.deltaTime * lookSpeed);
|
||||
lookAt.z = 0;
|
||||
lookAt.x = 0;
|
||||
gameObject.transform.rotation = lookAt;
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AutoAim : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
public GameObject enemy;
|
||||
|
||||
public float lookSpeed = 200f;
|
||||
|
||||
public GameObject player;
|
||||
public Camera camera;
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
Vector3 direction = enemy.transform.position - camera.transform.position;
|
||||
Quaternion targetRotation = Quaternion.LookRotation(direction);
|
||||
Quaternion lookAt = Quaternion.RotateTowards(camera.transform.rotation, targetRotation, Time.deltaTime * lookSpeed);
|
||||
camera.transform.rotation = lookAt;
|
||||
|
||||
}
|
||||
}
|
@ -24,13 +24,6 @@ public class Shooting : MonoBehaviour
|
||||
public GameObject gun;
|
||||
void Update()
|
||||
{
|
||||
/*if (Input.GetButtonDown("Fire1"))
|
||||
{
|
||||
Prefab = Random.Range(0, 1);
|
||||
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
||||
Shoot();
|
||||
}*/
|
||||
//if(Input.GetButtonDown("")
|
||||
if (Input.GetMouseButton(0) && fireCountdown <= 0f)
|
||||
{
|
||||
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
||||
@ -41,27 +34,14 @@ public class Shooting : MonoBehaviour
|
||||
fireCountdown -= Time.deltaTime;
|
||||
}
|
||||
|
||||
void Shoot()
|
||||
public void Shoot()
|
||||
{
|
||||
RaycastHit hit;
|
||||
if (Physics.Raycast(raycast.transform.position, raycast.transform.forward, out hit, range))
|
||||
if (Physics.Raycast(raycast.transform.position, raycast.transform.forward, out var hit, range))
|
||||
{
|
||||
Debug.Log(hit.transform.name);
|
||||
|
||||
Target target = hit.transform.GetComponent<Target>();
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
if (hit.transform.TryGetComponent<Target>(out var target))
|
||||
target.TakeDamage(damage);
|
||||
}
|
||||
RotateToGunDirection(gun, hit.point);
|
||||
}
|
||||
}
|
||||
|
||||
void RotateToGunDirection(GameObject obj, Vector3 destination)
|
||||
{
|
||||
direction = destination - obj.transform.position;
|
||||
rotation = Quaternion.LookRotation(direction);
|
||||
obj.transform.localRotation = Quaternion.Lerp(obj.transform.rotation, rotation, 1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user