Advertisement
Guest User

Unity User Control Standart Asset

a guest
Jun 19th, 2018
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 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 float m_Turn;
  15.         private float m_Turn_Last;
  16.         private bool m_Jump;                      // the world-relative desired move direction, calculated from the camForward and user input.
  17.  
  18.  
  19.         private void Start()
  20.         {
  21.             // get the transform of the main camera
  22.             if (Camera.main != null)
  23.             {
  24.                 m_Cam = Camera.main.transform;
  25.             }
  26.             else
  27.             {
  28.                 Debug.LogWarning(
  29.                     "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
  30.                 // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
  31.             }
  32.  
  33.             // get the third person character ( this should never be null due to require component )
  34.             m_Character = GetComponent<ThirdPersonCharacter>();
  35.         }
  36.  
  37.  
  38.         private void Update()
  39.         {
  40.             if (!m_Jump)
  41.             {
  42.                 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
  43.             }
  44.         }
  45.  
  46.  
  47.         // Fixed update is called in sync with physics
  48.         private void FixedUpdate()
  49.         {
  50.             // read inputs
  51.             float h = CrossPlatformInputManager.GetAxis("Horizontal");
  52.             float v = CrossPlatformInputManager.GetAxis("Vertical");
  53.             bool crouch = Input.GetKey(KeyCode.C);
  54.  
  55.             // calculate move direction to pass to character
  56.             if (m_Cam != null)
  57.             //if (false) // Já nechci relativni pohyb vuci kamere
  58.             {
  59.                 // calculate camera relative direction to move:
  60.                 m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
  61.                 m_Move = v*m_CamForward + h*m_Cam.right;
  62.  
  63.                 // když je ekvipnutá zbran, chci s ni mirit i kdyz stojim chci otacet s panackem
  64.                 m_Turn = m_Cam.eulerAngles[1];
  65.  
  66.  
  67.             }
  68.             else
  69.             {
  70.                 // we use world-relative directions in the case of no main camera
  71.                 m_Move = v*Vector3.forward + h*Vector3.right;
  72.                 m_Turn = 0;
  73.                 //turnIntegratorTest = h;
  74.             }
  75. //#if !MOBILE_INPUT
  76.             // walk speed multiplier
  77.             if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 2.5f;
  78. //#endif
  79.  
  80.             // pass all parameters to the character control script
  81.             m_Character.Move(m_Move, crouch, m_Jump);
  82.             //m_Character.Turn ((m_Turn-m_Turn_Last)/10);
  83.             //m_Turn_Last = m_Turn;
  84.             //m_Character.Turn(h);
  85.             m_Jump = false;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement