前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1.2D小游戏开发实记-群英争霸

1.2D小游戏开发实记-群英争霸

作者头像
祝你万事顺利
发布2019-06-25 10:19:52
3830
发布2019-06-25 10:19:52
举报
文章被收录于专栏:Unity游戏开发

1.云彩左右飘动效果

代码语言:javascript
复制
public class SimpleMove : MonoBehaviour
{
    private Vector3 originPos;
    private Vector3 towardsPos;
    private Vector3 curTarget;

    private void Start()
    {
        originPos = transform.position;
        towardsPos = transform.position + new Vector3(3, 0, 0);
        curTarget = towardsPos;
    }

    private void Update()
    {
        if (Vector3.Distance(transform.position,curTarget)<0.1f)
        {
            //到了当前的tar
            if (curTarget == towardsPos)
            {
                curTarget = originPos;
            }
            else
            {
                curTarget = towardsPos;
            }
        }
        Vector3 forward = Vector3.Normalize(curTarget - transform.position) * Time.deltaTime;
        transform.position += forward;
    }
}

2.制作按钮-可拓展

代码语言:javascript
复制
public class MyButton : MonoBehaviour
{
    public bool isPressing = false;//一直按着按钮为true
    public bool onPressed = false;//按下按钮为true
    public bool onReleased = false;//松开按钮为true

    private bool curState = false;
    private bool lastState = false;

    public void Tick(bool input)
    {
        curState = input;
        isPressing = curState;
        onPressed = false;
        onReleased = false;

        if (curState != lastState)
        {
            if (curState)
            {
                onPressed = true;
            }
            else
            {
                onReleased = true;
            }
        }
        lastState = curState;
    }
}

3.用户输入 将用户的输入转换成虚拟信号

代码语言:javascript
复制
public class KeyAndCoardInput : MonoBehaviour
{
    [Header("===== Key Setting =====")]
    public string keyLeft = "a";
    public string keyRight = "d";
    public string keyJump = "space";
    public string keyAtk01;
    public string keyAtk02;

    [Header("===== Output signals =====")]
    public float Dright;
    public bool jump;
    public bool attack01;
    

    public MyButton buttonJ = new MyButton();
    public MyButton buttonSpace = new MyButton();

    // Update is called once per frame
    void Update()
    {
        buttonJ.Tick(Input.GetKey(keyAtk01));
        buttonSpace.Tick(Input.GetKey(keyJump));

        attack01 = buttonJ.onPressed;
        jump = buttonSpace.onPressed;

        //移动后期可以添加SmoothDamp处理
        Dright = ((Input.GetKey(keyRight) ? 1.0f : 0) - (Input.GetKey(keyLeft) ? 1.0f : 0));
    }
}

4.清空Trigger 在idle状态中AddBehavior,进入idle状态重置atk的trigger

代码语言:javascript
复制
public class FSMClearSignals : StateMachineBehaviour
{
    public string[] clearTriggerAtEnter;
    public string[] clearTriggerAtExit;
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        foreach (string item in clearTriggerAtEnter)
        {
            animator.ResetTrigger(item);
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.06.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档