Advertisement
Guest User

PlayerMoveController

a guest
Nov 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6.  
  7. public class PlayerMoveController : MonoBehaviour {
  8.  
  9.     // PUBLIC
  10.     public SimpleTouchController leftController;
  11.     public Transform _rotation;
  12.  
  13.     public Animator animate;
  14.  
  15.     public RectTransform _jost,_center;
  16.  
  17.     public GameObject pers;
  18.  
  19.     public SimpleTouchController rightController;
  20.     public float speedMovements = 5f;
  21.     public float speedContinuousLook = 100f;
  22.     public float speedProgressiveLook = 3000f;
  23.     public bool continuousRightController = true;
  24.  
  25.     // PRIVATE
  26.     private Rigidbody _rigidbody;
  27.     private Vector2 localEulRot;
  28.     private Vector2 prevRightTouchPos;
  29.  
  30.     void Awake()
  31.     {
  32.  
  33.         _rigidbody = GetComponent<Rigidbody>();
  34.         //rightController.TouchEvent += RightController_TouchEvent;
  35.         //rightController.TouchStateEvent += RightController_TouchStateEvent;
  36.     }
  37.     public bool ContinuousRightController
  38.     {
  39.         set{continuousRightController = value;}
  40.     }
  41.  
  42.     void RightController_TouchStateEvent (bool touchPresent)
  43.     {
  44.         if(!continuousRightController)
  45.         {
  46.             prevRightTouchPos = Vector2.zero;
  47.         }
  48.     }
  49.  
  50.     void RightController_TouchEvent (Vector2 value)
  51.     {
  52.         if(!continuousRightController)
  53.         {
  54.             Vector2 deltaValues = value - prevRightTouchPos;
  55.             prevRightTouchPos = value;
  56.         }
  57.     }
  58.     void FixedUpdate()
  59.     {
  60.         animate.SetBool ("Walk", true);// move
  61.         _rigidbody.MovePosition(transform.position + (transform.up * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
  62.             (transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );
  63.  
  64.         if (continuousRightController)
  65.         {
  66.             transform.localEulerAngles = new Vector2(transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook,
  67.                 transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook);
  68.         }
  69.  
  70.         if(Mathf.Abs(_center.anchoredPosition.x) > 0 || Mathf.Abs(_center.anchoredPosition.y) > 0)
  71.         {
  72.             Vector2 heading = (Vector2)_center.anchoredPosition;
  73.             Vector2 direction = heading / heading.magnitude;
  74.  
  75.             float sdx = direction.x;
  76.             float sdy = direction.y;
  77.  
  78.             float sR = Mathf.Atan2(sdx, sdy);
  79.             float sD = 360 * sR / (2 * Mathf.PI);
  80.  
  81.             _rotation.transform.rotation = Quaternion.Euler(0.0f, 0.0f, -sD);
  82.         }
  83.         else
  84.         {
  85.             animate.SetBool ("Walk", false);
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement