Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- public class ModulRotateController : ModulBase
- {
- [Header("Обьекты вращения")]
- [Tooltip("Обьект вращения по оси Х")]
- public Transform transformXRotation;
- [Tooltip("Обьект вращения по оси Y")]
- public Transform transformYRotation;
- public ConfigController config;
- private float tempAngle = 0;
- private float currentSpeedRotation;
- private Vector3 _gunTarget;
- public Vector3 GunTarget
- {
- get { return _gunTarget; }
- set { _gunTarget = value; }
- }
- protected override void Start()
- {
- base.Start();
- CreateNextModul(config);
- }
- protected override void LoadConfig()
- {
- switch (botController.classBot)
- {
- case BotControllerBase.ClassBot.StormTrooper:
- {
- ConfigListSTModul tmp = Resources.Load("Config/ConfigListSTModul") as ConfigListSTModul;
- if (modulType == ConfigModulBase.ModulType.ControllerL)
- {
- config = tmp.configControllerL[modulLvl];
- return;
- }
- if (modulType == ConfigModulBase.ModulType.ControllerR)
- {
- config = tmp.configControllerR[modulLvl];
- return;
- };
- break;
- }
- case BotControllerBase.ClassBot.Scout:
- {
- ConfigListScoutModul tmp = Resources.Load("Config/ConfigListScoutModul") as ConfigListScoutModul;
- if (modulType == ConfigModulBase.ModulType.ControllerM)
- {
- config = tmp.configController[modulLvl];
- return;
- }
- break;
- }
- }
- }
- public override ModulRotateController GetModulRotateController()
- {
- return this;
- }
- public override Socket[] GetSocketArray()
- {
- return config.socketArray;
- }
- public override Socket GetCurrentSocket()
- {
- return null;
- }
- public override GameObject GetUIPrefab()
- {
- return config.UIPrefab;
- }
- public override ConfigModulBase.ModulType GetModulType()
- {
- return config.modulType;
- }
- protected override void OnEnable()
- {
- base.OnEnable();
- GunTarget = transform.TransformPoint(Vector3.forward * 100);
- }
- private void LateUpdate()
- {
- if (ModulStatus == Status.On)
- {
- switch (config.axisRotation)
- {
- case AxisRotation.AxisX:
- {
- if (transformXRotation != null)
- {
- RotationAxisX(transformXRotation, GetDirection(transformXRotation), SpeedRotation());
- }
- break;
- }
- case AxisRotation.AxisY:
- {
- if (transformYRotation != null)
- {
- RotationAxisY(transformYRotation, GetDirection(transformYRotation), SpeedRotation());
- }
- break;
- }
- case AxisRotation.AxisXY:
- {
- RotationAxisX(transformXRotation, GetDirection(transformXRotation), SpeedRotation());
- RotationAxisY(transformYRotation, GetDirection(transformYRotation), SpeedRotation());
- break;
- }
- }
- botController.CmdSetLookDirection(config.modulType, GunTarget, config.speedRotation);
- return;
- }
- /*
- if (transformXRotation != null)
- {
- RotationAxisX(transformXRotation, GetDirection(transformXRotation), currentSpeedRotation);
- }
- if (transformYRotation != null)
- {
- RotationAxisY(transformYRotation, GetDirection(transformYRotation), currentSpeedRotation);
- }
- */
- }
- private float SpeedRotation()
- {
- return config.speedRotation * Time.deltaTime;
- }
- private Quaternion GetDirection(Transform tempTransform)
- {
- //вектор, куда нужно вращать
- //Debug.DrawLine(GunTarget, tempTransform.position, Color.cyan);
- return Quaternion.LookRotation(GunTarget - tempTransform.position);
- }
- public override void SetLookDirection(Vector3 pointTarget)
- {
- GunTarget = pointTarget;
- }
- public override void SetLookDirection(Vector3 valueDirection, float valueSpeed)
- {
- GunTarget = valueDirection;
- currentSpeedRotation = valueSpeed;
- }
- private void RotationAxisY(Transform rotateTransform, Quaternion direction, float speed)
- {
- rotateTransform.rotation = Quaternion.RotateTowards(rotateTransform.rotation, direction, speed);
- tempAngle = rotateTransform.localEulerAngles.y;
- if (config.maxAngleY > 0)
- {
- if (rotateTransform.localEulerAngles.y > 180f)
- {
- //если значение Y больше 180, то используем ограничение между 360 и максимальним ограничением
- tempAngle = Mathf.Clamp(tempAngle, 360f - config.maxAngleY, 360f);
- }
- else
- {
- if (rotateTransform.localEulerAngles.y < 180f)
- {
- //если значение Y меньше 180, то ограничение используем между 0 и минимальным ограничением
- tempAngle = Mathf.Clamp(tempAngle, 0f, config.maxAngleY);
- }
- }
- }
- transformYRotation.localEulerAngles = new Vector3(0, tempAngle, 0);
- }
- private void RotationAxisX(Transform rotateTransform, Quaternion direction, float speed)
- {
- rotateTransform.rotation = Quaternion.RotateTowards(rotateTransform.rotation, direction, speed);
- tempAngle = rotateTransform.localEulerAngles.x;
- if (config.maxAngleX > 0)
- {
- if (rotateTransform.localEulerAngles.x > 180f)
- {
- tempAngle = Mathf.Clamp(tempAngle, 360f - config.maxAngleX, 360f);
- }
- else
- {
- if (rotateTransform.localEulerAngles.x < 180f)
- {
- tempAngle = Mathf.Clamp(tempAngle, 0f, config.maxAngleX);
- }
- }
- }
- transformXRotation.localEulerAngles = new Vector3(tempAngle, 0, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement