Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using GeekGame.Input;
- public class CubeControl : MonoBehaviour {
- public float speed=.1f;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- transform.Translate(new Vector3(JoystickMove.instance.H,0f,JoystickMove.instance.V)*speed*Time.deltaTime);
- /*
- transform.LookAt(transform.position+new Vector3(JoystickRotate.instance.H,0f,JoystickRotate.instance.V));
- */
- transform.LookAt(transform.position + new Vector3(JoystickRotate.instance.H, 0f, JoystickRotate.instance.V));
- if (JoystickFire.instance.Fire){
- Debug.Log("fire");
- }
- }
- }
- using UnityEngine;
- using System.Collections;
- using UnityEngine.EventSystems;
- namespace GeekGame.Input{
- public class JoystickMove : MonoBehaviour ,IDragHandler,IEndDragHandler
- {
- // remember turning is with joystick rotate
- // calculate the h and the v, then give it to PlayerMovement
- public static JoystickMove instance=null;
- public float _speed=6f;
- [Tooltip("the joystick radius ")]
- public float R=50f;
- private float _r;
- private Vector2 centerPos;
- private float _h;
- private float _v;
- public float H{
- get{return _h;}
- }
- public float V{
- get{return _v;}
- }
- void Awake()
- {
- if(instance!=null)
- {
- Destroy(this.gameObject);
- }
- else
- {
- instance=this;
- }
- }
- void Start(){
- _r=1f*Screen.width/960f*R; //this to calculate the scale of screen
- centerPos=GetComponent<RectTransform>().position;
- }
- void SetHAndF(Vector2 pos){ //Horizontall and Vertical axes
- Vector2 diff = pos-centerPos;
- float distance = diff.magnitude;
- if(distance>_r)
- {
- pos = centerPos+diff / distance*_r;
- }
- GetComponent<RectTransform>().position=pos;
- Vector2 move = pos-centerPos;
- _h = move.x;
- _v = move.y;
- }
- public void OnDrag(PointerEventData data)
- {
- Vector2 newPos =new Vector2(data.position.x-30f,data.position.y-30f);
- //clamp the sprite
- SetHAndF(newPos);
- }
- public void OnEndDrag(PointerEventData data){
- Debug.Log("End Drag"+centerPos);
- GetComponent<RectTransform>().position=centerPos;
- SetHAndF(centerPos);
- }
- // handle joystick motion events
- case SDL_JOYAXISMOTION:
- // check left-right movement
- if( event->jaxis.axis == 0 )
- {
- // if joystick moving left
- if ( event->jaxis.value < -800 )
- {
- // move left
- eventStates[JOYSTICK_LEFT] = 1;
- }
- else
- {
- eventStates[JOYSTICK_LEFT] = 0;
- }
- // if joystick moving right
- if ( event->jaxis.value > 800 )
- {
- // move right
- eventStates[JOYSTICK_RIGHT] = 1;
- }
- else
- {
- eventStates[JOYSTICK_RIGHT] = 0;
- }
- }
- // check up-down movement
- if( event->jaxis.axis == 1 )
- {
- // if joystick moving up
- if ( event->jaxis.value < -800 )
- {
- // move up
- eventStates[JOYSTICK_UP] = 1;
- }
- else
- {
- eventStates[JOYSTICK_UP] = 0;
- }
- // if joystick moving down
- if ( event->jaxis.value > 800 )
- {
- // move down
- eventStates[JOYSTICK_DOWN] = 1;
- }
- else
- {
- eventStates[JOYSTICK_DOWN] = 0;
- }
- }
- eventStates[JOYSTICK_SCALE] = event->jaxis.value;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement