Advertisement
zzosdunk

PrTopDownCharController

Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 25.08 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. //using PrUtils;
  6.  
  7. [RequireComponent(typeof(Rigidbody))]
  8. [RequireComponent(typeof(CapsuleCollider))]
  9. [RequireComponent(typeof(Animator))]
  10.  
  11. public class PrTopDownCharController : MonoBehaviour
  12. {
  13.  
  14.     [Header("Multiplayer")]
  15.     public int playerNmb = 1;
  16.     public PrPlayerSettings playerSettings;
  17.     public Renderer playerSelection;
  18.     //Inputs
  19.     [HideInInspector]
  20.     public string[] playerCtrlMap = {"Horizontal", "Vertical", "LookX", "LookY","FireTrigger", "Reload",
  21.         "EquipWeapon", "Sprint", "Aim", "ChangeWTrigger", "Roll", "Use", "Crouch", "ChangeWeapon", "Throw"  ,"Fire", "Mouse ScrollWheel"};
  22.  
  23.     [Header("Movement")]
  24.     [SerializeField] float m_JumpPower = 12f;
  25.     [Range(1f, 4f)] [SerializeField] float m_GravityMultiplier = 2f;
  26.     [SerializeField] float m_MoveSpeedMultiplier = 1f;
  27.     [HideInInspector]
  28.     public float m_MoveSpeedSpecialModifier = 1f;
  29.     [SerializeField] float m_AnimSpeedMultiplier = 1f;
  30.     private float m_GroundCheckDistance = 0.25f;
  31.  
  32.     public bool useRootMotion = true;
  33.  
  34.     public float PlayerRunSpeed = 1f;
  35.     public float PlayerAimSpeed = 1f;
  36.     public float PlayerSprintSpeed = 1f;
  37.     public float PlayerCrouchSpeed = 0.75f;
  38.  
  39.     public float RunRotationSpeed = 100f;
  40.     public float AimingRotationSpeed = 25f;
  41.  
  42.     public float AnimatorRunDampValue = 0.25f;
  43.     public float AnimatorSprintDampValue = 0.2f;
  44.     public float AnimatorAimingDampValue = 0.1f;
  45.  
  46.     Rigidbody m_Rigidbody;
  47.     Animator charAnimator;
  48.     bool m_IsGrounded;
  49.     float m_OrigGroundCheckDistance;
  50.     const float k_Half = 0.5f;
  51.     float m_TurnAmount;
  52.     float m_ForwardAmount;
  53.     float m_CapsuleHeight;
  54.     Vector3 m_CapsuleCenter;
  55.     CapsuleCollider m_Capsule;
  56.     bool m_Crouching;
  57.     private bool crouch = false;
  58.  
  59.     private bool b_CanRotate = true;
  60.     private bool m_Jump;
  61.     private float lastJump = 0.0f;
  62.     private bool b_canJump = true;
  63.     [HideInInspector] public bool Jumping = false;
  64.     [HideInInspector] public bool Sprinting = false;
  65.     [HideInInspector] public bool Rolling = false;
  66.  
  67.     public float RollStaminaUse = 0.5f;
  68.  
  69.     public enum EAction { Jump, Roll }
  70.     public EAction evadeAction = EAction.Jump;
  71.  
  72.     [HideInInspector] public bool m_isDead = false;
  73.     [HideInInspector] public bool m_CanMove = true;
  74.  
  75.     [Header("Aiming")]
  76.     public GameObject AimTargetVisual;
  77.     public Transform AimFinalPos;
  78.     public PrTopDownCamera CamScript;
  79.  
  80.     private Transform m_Cam;                  // A reference to the main camera in the scenes transform
  81.     [HideInInspector]
  82.     public Vector3 m_Move;                    // the world-relative desired move direction, calculated from the camForward and user input.
  83.     private Vector3 smoothMove;
  84.  
  85.     [Header("Joystick / Keyboard")]
  86.     public bool JoystickEnabled = true;
  87.     private GameObject JoystickTarget;
  88.     private GameObject JoystickLookRot;
  89.  
  90.     [Header("VFX")]
  91.     public GameObject RollVFX;
  92.  
  93.     private PrTopDownCharInventory Inventory;
  94.  
  95.     public List<GameObject> friends;
  96.  
  97.     void Start()
  98.     {
  99.         Inventory = GetComponent<PrTopDownCharInventory>();
  100.  
  101.         JoystickTarget = new GameObject();
  102.         JoystickTarget.name = "JoystickTarget";
  103.         JoystickTarget.transform.position = transform.position;
  104.         JoystickTarget.transform.parent = transform.parent;
  105.  
  106.         JoystickLookRot = new GameObject();
  107.         JoystickLookRot.name = "JoystickLookRotation";
  108.         JoystickLookRot.transform.position = transform.position;
  109.         JoystickLookRot.transform.parent = transform;
  110.  
  111.         // get the transform of the main camera
  112.         if (Camera.main != null)
  113.         {
  114.             m_Cam = CamScript.transform.GetComponentInChildren<Camera>().transform;
  115.         }
  116.         else
  117.         {
  118.             Debug.LogWarning(
  119.                 "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.");
  120.             // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
  121.         }
  122.  
  123.         charAnimator = GetComponent<Animator>();
  124.         m_Rigidbody = GetComponent<Rigidbody>();
  125.         m_Capsule = GetComponent<CapsuleCollider>();
  126.         m_CapsuleHeight = m_Capsule.height;
  127.         m_CapsuleCenter = m_Capsule.center;
  128.  
  129.         m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
  130.         m_OrigGroundCheckDistance = m_GroundCheckDistance;
  131.  
  132.         SetMultiplayerSettings();
  133.  
  134.         if (playerSettings)
  135.         {
  136.             playerCtrlMap = PrUtils.SetMultiplayerInputs(playerNmb, playerSettings, playerCtrlMap);
  137.  
  138.             if (playerNmb > 1)
  139.             {
  140.                 JoystickEnabled = true;
  141.             }
  142.         }
  143.  
  144.  
  145.         if (JoystickEnabled)
  146.             ActivateJoystick(true);
  147.         else
  148.             ActivateJoystick(false);
  149.  
  150.         friends = new List<GameObject>(GameObject.FindGameObjectsWithTag("AIPlayer"));
  151.         if (friends.Count != 0)
  152.         {
  153.             foreach (GameObject f in friends)
  154.             {
  155.                 f.GetComponent<PrEnemyAI>().FindPlayers();
  156.             }
  157.         }
  158.  
  159.         //Start PlayerInfo to Load and Save player Info across levels
  160.         /*if (GameObject.Find("playerInfo_" + playerNmb))
  161.         {
  162.             Debug.Log("Player Info Found");
  163.             LoadPlayerInfo();
  164.         }*/
  165.     }
  166.  
  167.     public void LoadPlayerInfo()
  168.     {
  169.         Debug.Log("player Info found - Loading Info");
  170.  
  171.         JoystickEnabled = PrPlayerInfo.player1.usingJoystick;
  172.         Inventory.Health = PrPlayerInfo.player1.health;
  173.         Inventory.ActualHealth = PrPlayerInfo.player1.actualHealth;
  174.         for (int i = 0; i < PrPlayerInfo.player1.weapons.Length; i++)
  175.         {
  176.             Inventory.InitialWeapons[i] = Inventory.WeaponListObject.weapons[PrPlayerInfo.player1.weapons[i]].GetComponent<PrWeapon>();
  177.             Inventory.grenadesCount = PrPlayerInfo.player1.grenades;
  178.         }
  179.     }
  180.  
  181.     public void SavePlayerInfo()
  182.     {
  183.         Debug.Log("Saving Player Info");
  184.  
  185.         PrPlayerInfo playerI = PrPlayerInfo.player1.GetComponent<PrPlayerInfo>();
  186.         playerI.playerNumber = playerNmb;
  187.         playerI.usingJoystick = JoystickEnabled;
  188.         playerI.playerName = Inventory.name;
  189.         playerI.health = Inventory.Health;
  190.         playerI.actualHealth = Inventory.ActualHealth;
  191.         playerI.maxWeaponCount = Inventory.playerWeaponLimit;
  192.         playerI.weapons = new int[Inventory.playerWeaponLimit];
  193.         playerI.weaponsAmmo = new int[Inventory.playerWeaponLimit];
  194.         playerI.weaponsClips = new int[Inventory.playerWeaponLimit];
  195.         playerI.grenades = Inventory.grenadesCount;
  196.  
  197.         for (int i = 0; i < Inventory.playerWeaponLimit; i++)
  198.         {
  199.             //Debug.Log("Weapon " + i + " is " + Inventory.Weapon[i] + " And the Name is " + Inventory.Weapon[i].GetComponent<PrWeapon>().WeaponName + " And the bullets are " + Inventory.Weapon[i].GetComponent<PrWeapon>().ActualBullets);
  200.             playerI.weapons[i] = Inventory.actualWeaponTypes[i];
  201.             playerI.weaponsAmmo[i] = Inventory.Weapon[i].GetComponent<PrWeapon>().ActualBullets;
  202.             playerI.weaponsClips[i] = Inventory.Weapon[i].GetComponent<PrWeapon>().ActualClips;
  203.         }
  204.     }
  205.  
  206.  
  207.     public void CreatePlayerInfo()
  208.     {
  209.         //Create Player info to be able to save player stats during gameplay
  210.  
  211.         Debug.Log("player Info NOT found - Saving Info");
  212.  
  213.         GameObject playerInfo = new GameObject("playerInfo_" + playerNmb);
  214.         playerInfo.AddComponent<PrPlayerInfo>();
  215.         PrPlayerInfo playerI = playerInfo.GetComponent<PrPlayerInfo>();
  216.         playerI.playerNumber = playerNmb;
  217.         playerI.usingJoystick = JoystickEnabled;
  218.         playerI.playerName = Inventory.name;
  219.         playerI.health = Inventory.Health;
  220.         playerI.actualHealth = Inventory.ActualHealth;
  221.         playerI.maxWeaponCount = Inventory.playerWeaponLimit;
  222.         playerI.weapons = new int[Inventory.playerWeaponLimit];
  223.         playerI.weaponsAmmo = new int[Inventory.playerWeaponLimit];
  224.         playerI.weaponsClips = new int[Inventory.playerWeaponLimit];
  225.  
  226.         for (int i = 0; i < Inventory.playerWeaponLimit; i++)
  227.         {
  228.             //Debug.Log("Weapon " + i + " is " + Inventory.Weapon[i] + " And the Name is " + Inventory.Weapon[i].GetComponent<PrWeapon>().WeaponName + " And the bullets are " + Inventory.Weapon[i].GetComponent<PrWeapon>().ActualBullets);
  229.             playerI.weapons[i] = Inventory.actualWeaponTypes[i];
  230.             playerI.weaponsAmmo[i] = Inventory.Weapon[i].GetComponent<PrWeapon>().ActualBullets;
  231.             playerI.weaponsClips[i] = Inventory.Weapon[i].GetComponent<PrWeapon>().ActualClips;
  232.         }
  233.  
  234.     }
  235.  
  236.     /*
  237.     public void SetMultiplayerInputs()
  238.     {
  239.         if (playerNmb > 1)
  240.         {
  241.             //Multiplayer only works with joystick input
  242.             JoystickEnabled = true;
  243.  
  244.             int values = 0;
  245.             foreach (string ctrl in playerSettings.playerCtrlMap)
  246.             {
  247.                 playerCtrlMap[values] = ctrl + playerNmb.ToString();
  248.                 values += 1;
  249.             }
  250.  
  251.         }
  252.         else
  253.         {
  254.             playerCtrlMap = playerSettings.playerCtrlMap;
  255.         }
  256.        
  257.     }*/
  258.  
  259.  
  260.     public void SetMultiplayerSettings()
  261.     {
  262.         if (playerSelection)
  263.         {
  264.             playerSelection.material.SetColor("_TintColor", playerSettings.playerColor[playerNmb - 1]);
  265.         }
  266.  
  267.     }
  268.  
  269.     public void ActivateJoystick(bool IsOn)
  270.     {
  271.         if (IsOn)
  272.             AimTargetVisual.SetActive(false);
  273.         else
  274.             AimTargetVisual.SetActive(true);
  275.     }
  276.  
  277.     public void EndRoll()
  278.     {
  279.         Rolling = false;
  280.         Jumping = false;
  281.         b_CanRotate = true;
  282.     }
  283.  
  284.     public void CanJump(int Value)
  285.     {
  286.         if (Value == 1)
  287.         {
  288.             b_canJump = true;
  289.         }
  290.         else
  291.         {
  292.             b_canJump = false;
  293.         }
  294.     }
  295.  
  296.     public void CantRotate()
  297.     {
  298.         b_CanRotate = false;
  299.     }
  300.  
  301.  
  302.  
  303.     // Update is called once per frame
  304.     void Update()
  305.     {
  306.  
  307.         //Time.timeScale = Slowmotion;
  308.  
  309.         if (Input.GetKeyDown(KeyCode.K) && playerNmb <= 1)
  310.         {
  311.             if (JoystickEnabled)
  312.                 JoystickEnabled = false;
  313.             else
  314.                 JoystickEnabled = true;
  315.  
  316.             ActivateJoystick(JoystickEnabled);
  317.         }
  318.  
  319.         MouseTargetPos();
  320.  
  321.         if (!m_isDead && m_CanMove)
  322.         {
  323.  
  324.             //Crouch
  325.             if (Input.GetKey(KeyCode.LeftControl) && playerNmb <= 1 || Input.GetButton(playerCtrlMap[12]))
  326.             {
  327.                 print(playerCtrlMap[13]);
  328.                 crouch = true;
  329.  
  330.             }
  331.             else
  332.             {
  333.                 crouch = false;
  334.             }
  335.  
  336.             float h = Input.GetAxis(playerCtrlMap[0]);
  337.             float v = Input.GetAxis(playerCtrlMap[1]);
  338.  
  339.             if (crouch && Inventory.Aiming)
  340.             {
  341.                 h = 0;
  342.                 v = 0;
  343.             }
  344.  
  345.             //Roll
  346.  
  347.             if (Input.GetButton(playerCtrlMap[10]) && !Rolling && !Inventory.UsingObject && Inventory.ActualStamina > RollStaminaUse)
  348.             {
  349.                 if (evadeAction == EAction.Roll)
  350.                 {
  351.                     Rolling = true;
  352.                     Inventory.Weapon[Inventory.ActiveWeapon].GetComponent<PrWeapon>().LaserSight.enabled = false;
  353.                     Inventory.Weapon[Inventory.ActiveWeapon].GetComponent<PrWeapon>().CancelReload();
  354.                     Inventory.ActualStamina -= RollStaminaUse;
  355.                     charAnimator.SetTrigger("Roll");
  356.                     if (RollVFX)
  357.                         Instantiate(RollVFX, transform.position, Quaternion.identity);
  358.                 }
  359.             }
  360.  
  361.             //Jump
  362.             if (Input.GetButton(playerCtrlMap[10]) && !Rolling && !m_Jump && !Inventory.UsingObject && !crouch && Time.time >= lastJump + 0.2f && m_IsGrounded && b_canJump)
  363.             {
  364.  
  365.                 if (evadeAction == EAction.Jump && !charAnimator.GetCurrentAnimatorStateInfo(0).IsName("JumpEnd"))
  366.                 {
  367.  
  368.                     lastJump = Time.time;
  369.                     Rolling = true;
  370.                     m_Jump = true;
  371.                     Inventory.Weapon[Inventory.ActiveWeapon].GetComponent<PrWeapon>().LaserSight.enabled = false;
  372.                     Inventory.Weapon[Inventory.ActiveWeapon].GetComponent<PrWeapon>().CancelReload();
  373.                     charAnimator.SetTrigger("Jump");
  374.                     if (RollVFX)
  375.                         Instantiate(RollVFX, transform.position, Quaternion.identity);
  376.  
  377.                 }
  378.             }
  379.  
  380.  
  381.             if (b_CanRotate)
  382.             {
  383.                 if (Inventory.Aiming && !Rolling)
  384.                 {
  385.                     if (!JoystickEnabled)
  386.                         MouseAim(AimFinalPos.position);
  387.                     else
  388.                         JoystickLook(h, v);
  389.                 }
  390.                 else
  391.                 {
  392.                     RunningLook(new Vector3(h, 0, v));
  393.                 }
  394.             }
  395.  
  396.             m_Move = new Vector3(h, 0, v);// * m_MoveSpeedSpecialModifier;
  397.  
  398.  
  399.             m_Move = m_Move.normalized * m_MoveSpeedSpecialModifier;
  400.             //Rotate move in camera space
  401.             m_Move = Quaternion.Euler(0, 0 - transform.eulerAngles.y + m_Cam.transform.parent.transform.eulerAngles.y, 0) * m_Move;
  402.  
  403.             //Move Player
  404.             Move(m_Move, crouch, m_Jump);
  405.             m_Jump = false;
  406.  
  407.             //Sprint
  408.             if (Input.GetButton(playerCtrlMap[7]) && !Rolling && m_Move.magnitude >= 0.2f && !Inventory.UsingObject && !crouch)
  409.             {
  410.  
  411.  
  412.                 if (Inventory.ActualStamina > 0.0f)
  413.                 {
  414.                     Sprinting = true;
  415.  
  416.                     if (Inventory.alwaysAim)
  417.                     {
  418.                         Inventory.Aiming = false;
  419.                         charAnimator.SetBool("Aiming", false);
  420.                     }
  421.                 }
  422.                 else
  423.                 {
  424.                     Sprinting = false;
  425.                     if (Inventory.alwaysAim)
  426.                     {
  427.                         Inventory.Aiming = true;
  428.                         charAnimator.SetBool("Aiming", true);
  429.                     }
  430.                 }
  431.             }
  432.             else
  433.             {
  434.                 Sprinting = false;
  435.                 if (Inventory.alwaysAim)
  436.                 {
  437.                     Inventory.Aiming = true;
  438.                     charAnimator.SetBool("Aiming", true);
  439.                 }
  440.             }
  441.  
  442.             Inventory.UsingStamina = Sprinting;
  443.  
  444.         }
  445.         else
  446.         {
  447.             m_ForwardAmount = 0.0f;
  448.             m_TurnAmount = 0.0f;
  449.             Inventory.Aiming = false;
  450.             UpdateAnimator(Vector3.zero);
  451.         }
  452.  
  453.     }
  454.  
  455.     private void RunningLook(Vector3 Direction)
  456.     {
  457.         if (Direction.magnitude >= 0.25f)
  458.         {
  459.             Direction = Quaternion.Euler(0, 0 + m_Cam.transform.parent.transform.eulerAngles.y, 0) * Direction;
  460.  
  461.             if (!Rolling)
  462.                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Direction), Time.deltaTime * (RunRotationSpeed * 0.1f));
  463.             else
  464.                 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Direction), Time.deltaTime * RunRotationSpeed);
  465.  
  466.             transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0);
  467.         }
  468.  
  469.     }
  470.  
  471.     private void JoystickLook(float h, float v)
  472.     {
  473.         JoystickTarget.transform.rotation = transform.rotation;
  474.  
  475.         //Joystick Look input
  476.         float LookX = Input.GetAxis(playerCtrlMap[2]);
  477.         float LookY = Input.GetAxis(playerCtrlMap[3]);
  478.  
  479.         Vector3 JoystickLookVec = new Vector3(LookX, 0, LookY) * 10;
  480.  
  481.         JoystickLookVec = Quaternion.Euler(0, 0 + m_Cam.transform.parent.transform.eulerAngles.y, 0) * JoystickLookVec;
  482.  
  483.         JoystickTarget.transform.position = transform.position + JoystickLookVec * 5;
  484.  
  485.         if (Mathf.Abs(LookX) <= 0.2f && Mathf.Abs(LookY) <= 0.2f)
  486.         {
  487.             JoystickTarget.transform.localPosition += JoystickTarget.transform.forward * 2;
  488.         }
  489.  
  490.         JoystickLookRot.transform.LookAt(JoystickTarget.transform.position);
  491.  
  492.         AimTargetVisual.transform.position = JoystickTarget.transform.position;
  493.         AimTargetVisual.transform.LookAt(transform.position);
  494.  
  495.         transform.rotation = Quaternion.Lerp(transform.rotation, JoystickLookRot.transform.rotation, Time.deltaTime * AimingRotationSpeed);
  496.         transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0);
  497.  
  498.  
  499.  
  500.     }
  501.  
  502.  
  503.     private void MouseTargetPos()
  504.     {
  505.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  506.         RaycastHit hit;
  507.         if (Physics.Raycast(ray, out hit, 2000f, 9))
  508.         {
  509.             //Debug.Log("---------- Hit Something------------");
  510.             Vector3 FinalPos = new Vector3(hit.point.x, 0, hit.point.z);
  511.  
  512.             AimTargetVisual.transform.position = FinalPos;
  513.             AimTargetVisual.transform.LookAt(transform.position);
  514.  
  515.         }
  516.     }
  517.  
  518.     private void MouseAim(Vector3 FinalPos)
  519.     {
  520.         JoystickLookRot.transform.LookAt(FinalPos);
  521.         transform.rotation = Quaternion.Lerp(transform.rotation, JoystickLookRot.transform.rotation, Time.deltaTime * AimingRotationSpeed);
  522.         transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0);
  523.  
  524.     }
  525.  
  526.     void PlayFootStepAudio()
  527.     {
  528.  
  529.     }
  530.  
  531.     public void Move(Vector3 move, bool crouch, bool jump)
  532.     {
  533.         if (!Inventory.UsingObject)
  534.         {
  535.             CheckGroundStatus();
  536.  
  537.             m_TurnAmount = move.x;
  538.             m_ForwardAmount = move.z;
  539.  
  540.             // control and velocity handling is different when grounded and airborne:
  541.             if (m_IsGrounded)
  542.             {
  543.                 HandleGroundedMovement(crouch, jump);
  544.             }
  545.             else
  546.             {
  547.                 HandleAirborneMovement();
  548.             }
  549.  
  550.             ScaleCapsuleForCrouching(crouch);
  551.             PreventStandingInLowHeadroom();
  552.  
  553.             // send input and other state parameters to the animator
  554.             UpdateAnimator(move);
  555.         }
  556.  
  557.     }
  558.  
  559.  
  560.     void ScaleCapsuleForCrouching(bool crouch)
  561.     {
  562.         if (m_IsGrounded && crouch)
  563.         {
  564.             if (m_Crouching) return;
  565.             m_Capsule.height = m_Capsule.height / 1.5f;
  566.             m_Capsule.center = m_Capsule.center / 1.5f;
  567.             m_Crouching = true;
  568.         }
  569.         else
  570.         {
  571.             Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
  572.             float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
  573.             if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength))
  574.             {
  575.                 m_Crouching = true;
  576.                 return;
  577.             }
  578.             m_Capsule.height = m_CapsuleHeight;
  579.             m_Capsule.center = m_CapsuleCenter;
  580.             m_Crouching = false;
  581.         }
  582.     }
  583.  
  584.     void PreventStandingInLowHeadroom()
  585.     {
  586.         // prevent standing up in crouch-only zones
  587.         if (!m_Crouching)
  588.         {
  589.             Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
  590.             float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
  591.             if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength))
  592.             {
  593.                 m_Crouching = true;
  594.             }
  595.         }
  596.     }
  597.  
  598.  
  599.     void UpdateAnimator(Vector3 move)
  600.     {
  601.         // update the animator parameters
  602.         charAnimator.SetFloat("Y", m_ForwardAmount, AnimatorAimingDampValue, Time.deltaTime);
  603.         charAnimator.SetFloat("X", m_TurnAmount, AnimatorAimingDampValue, Time.deltaTime);
  604.  
  605.         if (!Sprinting)
  606.             charAnimator.SetFloat("Speed", move.magnitude, AnimatorSprintDampValue, Time.deltaTime);
  607.         else
  608.             charAnimator.SetFloat("Speed", 2.0f, AnimatorRunDampValue, Time.deltaTime);
  609.  
  610.         charAnimator.SetBool("Crouch", m_Crouching);
  611.         charAnimator.SetBool("OnGround", m_IsGrounded);
  612.  
  613.         // the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector,
  614.         // which affects the movement speed because of the root motion.
  615.         if (m_IsGrounded && move.magnitude > 0)
  616.         {
  617.  
  618.             if (Inventory.Aiming && !Rolling)
  619.             {
  620.                 move *= PlayerAimSpeed;
  621.                 transform.Translate(move * Time.deltaTime);
  622.                 charAnimator.applyRootMotion = false;
  623.             }
  624.             else if (Inventory.UsingObject)
  625.             {
  626.                 move = move * 0.0f;
  627.                 transform.Translate(Vector3.zero);
  628.                 charAnimator.applyRootMotion = false;
  629.             }
  630.             else
  631.             {
  632.                 if (useRootMotion)
  633.                     charAnimator.applyRootMotion = true;
  634.                 else
  635.                 {
  636.                     if (!Rolling)
  637.                     {
  638.                         if (Sprinting)
  639.                         {
  640.                             move *= PlayerSprintSpeed;
  641.                         }
  642.                         else if (crouch)
  643.                         {
  644.                             move *= PlayerCrouchSpeed;
  645.                         }
  646.                         else
  647.                         {
  648.                             move *= PlayerRunSpeed;
  649.                         }
  650.  
  651.                         transform.Translate(move * Time.deltaTime);
  652.                         charAnimator.applyRootMotion = false;
  653.                     }
  654.  
  655.                 }
  656.             }
  657.  
  658.             charAnimator.speed = m_AnimSpeedMultiplier;
  659.         }
  660.         else
  661.         {
  662.             // don't use that while airborne
  663.             charAnimator.speed = 1;
  664.         }
  665.     }
  666.  
  667.  
  668.     void HandleAirborneMovement()
  669.     {
  670.         // apply extra gravity from multiplier:
  671.         Vector3 extraGravityForce = (Physics.gravity * m_GravityMultiplier) - Physics.gravity;
  672.         m_Rigidbody.AddForce(extraGravityForce);
  673.  
  674.         m_GroundCheckDistance = m_Rigidbody.velocity.y < 0 ? m_OrigGroundCheckDistance : 0.01f;
  675.     }
  676.  
  677.  
  678.     void HandleGroundedMovement(bool crouch, bool jump)
  679.     {
  680.         // check whether conditions are right to allow a jump:
  681.  
  682.         if (jump && !crouch)
  683.         {
  684.             if (charAnimator.GetCurrentAnimatorStateInfo(0).IsName("Grounded") || charAnimator.GetCurrentAnimatorStateInfo(0).IsName("Grounded_Unarmed") || charAnimator.GetCurrentAnimatorStateInfo(0).IsName("Aiming")
  685.                 || charAnimator.GetCurrentAnimatorStateInfo(0).IsName("Grounded_Armed_Crouch") || charAnimator.GetCurrentAnimatorStateInfo(0).IsName("Grounded_Unarmed_Crouch"))
  686.             {
  687.                 // jump!
  688.                 //Debug.Log("-------------------JUMP---------------------");
  689.                 m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z);
  690.                 m_IsGrounded = false;
  691.                 charAnimator.applyRootMotion = false;
  692.                 m_GroundCheckDistance = 0.025f;
  693.             }
  694.  
  695.         }
  696.     }
  697.  
  698.     //This function it´s used only for Aiming and Jumping states. Those anims doesn´t have root motion so we move the player by script
  699.     public void OnAnimatorMove()
  700.     {
  701.         // we implement this function to override the default root motion.
  702.         // this allows us to modify the positional speed before it's applied.
  703.         if (m_IsGrounded && Time.deltaTime > 0)
  704.         {
  705.             Vector3 v = (charAnimator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
  706.  
  707.             // we preserve the existing y part of the current velocity.
  708.             v.y = m_Rigidbody.velocity.y;
  709.             m_Rigidbody.velocity = v;
  710.         }
  711.     }
  712.  
  713.     void CheckGroundStatus()
  714.     {
  715.         RaycastHit hitInfo;
  716. #if UNITY_EDITOR
  717.         // helper to visualise the ground check ray in the scene view
  718.         //Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance));
  719. #endif
  720.         // 0.1f is a small offset to start the ray from inside the character
  721.         // it is also good to note that the transform position in the sample assets is at the base of the character
  722.         if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance))
  723.         {
  724.             m_IsGrounded = true;
  725.             charAnimator.applyRootMotion = true;
  726.         }
  727.         else
  728.         {
  729.             m_IsGrounded = false;
  730.             charAnimator.applyRootMotion = false;
  731.         }
  732.     }
  733.  
  734.     void OnTriggerEnter(Collider other)
  735.     {
  736.         if (other.CompareTag("EnvZone"))
  737.         {
  738.             CamScript.TargetHeight = other.GetComponent<PrEnvironmentZone>().CameraHeight;
  739.         }
  740.  
  741.     }
  742.  
  743.  
  744. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement