第四回 血条的文本和一头雾水的动作修正

首先呢,血条文本比较简单,基本上和血条差不多的操作。


首先建一个text,然后就是输入血条数值。

接下来就全是代码的事情了

做得一头雾水

stat代码:

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


public class stat : MonoBehaviour {


    private Image content;
    [SerializeField]
    private Text statValue;


    [SerializeField]
    private float lerpSpeed;


    private float currentFill;


    public float MyMaxValue { get; set; }


    private float currentValue;


    public float MyCurrentValue
    {
        get
        {
            return currentValue;
        }


        set
        {
            if(value>MyMaxValue)
            {
                currentValue = MyMaxValue;
            }
            else if(value<0)
            {
                currentValue = 0;
            }
            else
            {
                currentValue = value;
            }
           
            currentFill = currentValue / MyMaxValue;
            statValue.text = currentValue + "/" + MyMaxValue;
        }
    }








// Use this for initialization
void Start () {
        content = GetComponent<Image>();
     
}

// Update is called once per frame
void Update ()
      {
        if (currentFill != content.fillAmount)
            {
            content.fillAmount = Mathf.Lerp(content.fillAmount, currentFill, Time.deltaTime * lerpSpeed);
            }
}
    public void Initialize(float currentValue, float maxValue)
    {
        MyMaxValue = maxValue;
        MyCurrentValue = currentValue;
    }
}


character代码:using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public abstract class character : MonoBehaviour {
    [SerializeField]
    private float speed;


    private Animator myAnimator;
    // Use this for initialization
    protected Vector2 direction;


    private Rigidbody2D myRigidbody;


    public bool IsMoving
    {
        get
        {
            return direction.x != 0 || direction.y != 0;
        }
    }


   protected virtual void Start() {
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
}

// Update is called once per frame
protected virtual void Update () {
       HandleLayers();
    }


    private void FixedUpdate()
    {
        Move();
    }
    public void Move()
    {
        myRigidbody.velocity = direction.normalized * speed;
        //x=20,y=2//Normalize =1,1
    }
    public void HandleLayers()
    {
        if (IsMoving)
        {
       
            ActivateLayer("WalkLayer");
            myAnimator.SetLayerWeight(1, 1);
            myAnimator.SetFloat("x", direction.x);
            myAnimator.SetFloat("y", direction.y);
        }
        else
        {
            ActivateLayer("IdleLayer");
        }


    }




    public void ActivateLayer(string layerName)
    {
        for(int i = 0;i <myAnimator.layerCount;i++)
        {
            myAnimator.SetLayerWeight(i, 0);
        }
        myAnimator.SetLayerWeight(myAnimator.GetLayerIndex(layerName), 1);


    }
}

player代码:

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


public class player : character {
   
    [SerializeField]
    private stat health;


    [SerializeField]
    private stat mana;
 
    [SerializeField]
    private float initHealth = 100;


    private float initMana = 50;
    protected override void Start()
    {
        health.Initialize(initHealth,initHealth);
        mana.Initialize(initMana, initMana);
        base.Start();
    }
    // Use this for initialization


        // Update is called once per frame
    protected override void Update()
    {
        GetInput();
        base.Update();
    }


    private void GetInput()
    {
        direction = Vector2.zero;
        //for debug
        if(Input.GetKeyDown(KeyCode.I))
        {
            health.MyCurrentValue -= 10;
            mana.MyCurrentValue -= 10;
        }
        if(Input.GetKeyDown(KeyCode.O))
        {
            health.MyCurrentValue += 10;
            mana.MyCurrentValue += 10;
        }
        //here


        if(Input.GetKey(KeyCode.W))
        {
            direction += Vector2.up;
        }
        if (Input.GetKey(KeyCode.A))
        {
            direction += Vector2.left;
        }
        if (Input.GetKey(KeyCode.S))
        {
            direction += Vector2.down;
        }
        if (Input.GetKey(KeyCode.D))
        {
            direction += Vector2.right;
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值