Unity3D 虛擬遙感角色控制(基於UGUI 非插件)

                                              Unity3D 虛擬遙感角色控制


  •  Unity3D 虛擬遙感角色控制, 基於UGUI 沒有使用插件;
  •  並且映射鍵盤 用於編輯狀態下的 調試
  •  Unity3D 版本 :Unity 2018.4.20f1 (64-bit)

 

 這次待業在準備 ,做個小的 RPG 遊戲,

 以前也實現過一次 虛擬遙感,但是忘記了;這次又重新來一次 記錄下過程, 簡單的連擊效果;

 下一個版本準備,把場景以及 攝像頭跟隨加上,

這裏主要使用到了三個腳本

  • JoyStickMove  控制 虛擬遙感的腳本
  • JoyStickFight   控制 按鈕Fight的腳本
  • HereController 角色的移動 以及動畫控制

 

JoyStickMove.CS

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class JoyStickMove : MonoBehaviour ,IBeginDragHandler,  IDragHandler, IEndDragHandler
{

    public float maxRadius = 150;
    private RectTransform upperSprite;
    private Vector2 originAnchoredPosition;
    private Vector2 vector2Move =Vector2.zero;
    private bool isDrag = false;

    public delegate void OnMoveStart();
    public event OnMoveStart onMoveStart;
    public delegate void OnMoving(Vector2 vector2Move);
    public event OnMoving onMoving;
    public delegate void OnMoveEnd();
    public event OnMoveEnd onMoveEnd;

    public delegate void OnRotat(float rotatY);
    public event OnRotat onRotat;


    void Start()
    {
        this.upperSprite = transform.GetChild(0).GetComponentInChildren<RectTransform>();
        this.originAnchoredPosition = upperSprite.anchoredPosition;    
    }

    public void OnDrag(PointerEventData eventData)
    {
        this.upperSprite.anchoredPosition += eventData.delta;
        
        this.upperSprite.anchoredPosition = Vector2.ClampMagnitude(this.upperSprite.anchoredPosition, this.maxRadius);
         
        this.vector2Move = this.upperSprite.anchoredPosition / this.maxRadius;

        if (onMoving != null)
        {
            onMoving(this.vector2Move);
        }

        if (onRotat != null)
        {
            onRotat(-Vector2.SignedAngle(new Vector2(0, 1), this.vector2Move));
        }
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        this.isDrag = true;
        if (this.onMoveStart != null)
        {
            onMoveStart();
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        this.isDrag = false;
        this.upperSprite.anchoredPosition = this.originAnchoredPosition;
        if (onMoveEnd != null)
        {
            onMoveEnd();
        }
    }
#if UNITY_EDITOR 
    public void Update()
    {
        if (!this.isDrag)
        {

            if (onMoving != null)
            {
                this.vector2Move.x = Input.GetAxis("Horizontal");
                this.vector2Move.y = Input.GetAxis("Vertical");

                if (this.vector2Move.x != 0 || this.vector2Move.y != 0)
                {
                    if (onMoveStart != null)
                    {
                        onMoveStart();
                    }
                    if (onMoving != null)
                    {
                        onMoving(this.vector2Move);
                    }
                    if (onRotat != null)
                    {
                        onRotat(-Vector2.SignedAngle(new Vector2(0, 1), this.vector2Move));
                    }

                }
                else
                {
                    if (onMoveEnd != null)
                    {
                        onMoveEnd();
                    }
                }

            }

        }
    }
#endif
}


 

 

JoyStickFight.CS

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class JoyStickFight : MonoBehaviour
{
    public delegate void OnFight();
    public event OnFight onFight;
    // Start is called before the first frame update
    void Start()
    {
        GetComponent<Button>().onClick.AddListener(onClick);
    }

    void onClick()
    {
        if (onFight != null)
        {
            onFight();
        }
    }
#if UNITY_EDITOR
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            if (onFight != null)
            {
                onFight();
            }
        }
    }
#endif



}

 

HereController.CS

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HereController : MonoBehaviour {
    [SerializeField]
    private Animator animator;
    private JoyStickMove joyStickMove;
    private JoyStickFight joyStickFight;
    public float moveSpeed = 1;
    private Vector3 detailMove;
    private float detailAngleY;

    private Boolean isFight;

    private void Awake()
    {
        this.animator = this.GetComponentInChildren<Animator>();

        this.joyStickMove = FindObjectOfType<JoyStickMove>();
        this.joyStickMove.onMoveStart += this.onMoveStart;
        this.joyStickMove.onMoving += this.onMoving;
        this.joyStickMove.onMoveEnd += this.onMoveEnd;
        this.joyStickMove.onRotat += this.OnRotat;

        this.joyStickFight = FindObjectOfType<JoyStickFight>();
        this.joyStickFight.onFight += this.onFight;



    }

    // Use this for initialization
    void Start () {
		
	}
    public void onMoveStart()
    {
        this.animator.SetBool("move", true);   
    }

    public void onMoving(Vector2 vector2)
    {        
        this.detailMove = new Vector3(vector2.x, 0, vector2.y);       
        this.animator.SetFloat("movePara", vector2.magnitude);
    }

    public void onMoveEnd()
    {
        this.detailMove = Vector2.zero;
        this.animator.SetBool("move", false);
    }


    private void OnRotat(float rotatY)
    {
        this.detailAngleY = rotatY;      
    }

    private void onFight()
    {

        AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);

        if (stateInfo.IsName("Attack1"))
        {
            if (stateInfo.normalizedTime > 0.6 && stateInfo.normalizedTime < 0.8)
            {
                this.animator.SetTrigger("fightIn_2");
            }
        }
        else if (stateInfo.IsName("Attack2"))
        {

            if (stateInfo.normalizedTime > 0.6 && stateInfo.normalizedTime < 0.8)
            {
                this.animator.SetTrigger("fightIn_3");
            }

        }
        else
        {
            this.animator.SetTrigger("fightIn");
        }

    }



    // Update is called once per frame
    void Update () {

        if (animator) {
            AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
            if (stateInfo.IsName("move") ) 
            {
                this.transform.Translate(this.detailMove * Time.deltaTime * moveSpeed, Space.World);

                this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.Euler(0, this.detailAngleY, 0), Time.time * 0.05f);
            };
        }     
    }
}

 

工程下載鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章