Advertisement
Guest User

ThirdPersonUserControl

a guest
Apr 2nd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityStandardAssets.CrossPlatformInput;
  4.  
  5. namespace UnityStandardAssets.Characters.ThirdPerson
  6. {
  7. [RequireComponent(typeof(ThirdPersonCharacter))]
  8. public class ThirdPersonUserControl : MonoBehaviour
  9. {
  10. private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
  11. private Transform m_Cam; // A reference to the main camera in the scenes transform
  12. private Vector3 m_CamForward; // The current forward direction of the camera
  13. private Vector3 m_Move;
  14. private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.
  15. public bool canMove=true;
  16. public int direct = 1;
  17. Vector3 pastcamf;
  18. float pastv;
  19.  
  20. private void Start()
  21. {
  22. // get the transform of the main camera
  23. if (Camera.main != null)
  24. {
  25. m_Cam = Camera.main.transform;
  26. }
  27. else
  28. {
  29. Debug.LogWarning(
  30. "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
  31. // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
  32. }
  33.  
  34. // get the third person character ( this should never be null due to require component )
  35. m_Character = GetComponent<ThirdPersonCharacter>();
  36. }
  37.  
  38.  
  39. private void Update()
  40. {
  41. if (!m_Jump)
  42. {
  43. m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
  44. }
  45. }
  46.  
  47. bool camflip(int angle) {
  48. int shit = (int) Vector3.SignedAngle(m_CamForward, pastcamf, Vector3.up);
  49. return (shit > angle || shit < -angle);
  50. }
  51.  
  52.  
  53. // Fixed update is called in sync with physics
  54. private void FixedUpdate()
  55. {
  56. // read inputs
  57. float h = CrossPlatformInputManager.GetAxis("Horizontal");
  58. float v = CrossPlatformInputManager.GetAxis("Vertical");
  59. bool crouch = Input.GetKey(KeyCode.C);
  60. // calculate move direction to pass to character
  61.  
  62. if (m_Cam != null)
  63. {
  64. // calculate camera relative direction to move:
  65. m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
  66. if (camflip(40)) { direct = -1;}
  67. m_Move = direct * v * m_CamForward + h * m_Cam.right;
  68. if (v < pastv || v == 0) { direct = 1; }
  69.  
  70.  
  71. pastcamf = m_CamForward;
  72. pastv = v;
  73. }
  74. else
  75. {
  76. // we use world-relative directions in the case of no main camera
  77. m_Move = v * Vector3.forward + h * Vector3.right;
  78. }
  79.  
  80. // pass all parameters to the character control script
  81. if (canMove)
  82. {
  83. m_Character.Move(m_Move, crouch, m_Jump);
  84. }
  85. m_Jump = false;
  86. }
  87.  
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement