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);
            };
        }     
    }
}

 

工程下载链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nicepainkiller

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值