第三回 呼吸动画及人物血条蓝条编辑
首先呢,由于呼吸动画是我很久之前学的了,现在已经忘得七七八八。只大概记得先把那四个面的帧动画做出来(控制器什么的),然后删除上方的多出来的图像,只保留控制器和动画。接下来复制一个Idle_down做为walk_down。
道理基本上是一样的,但是用呼吸动画覆盖了之前那个行走图像。
character那边的代码也会修改
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class character : MonoBehaviour {
[SerializeField]
private float speed;
private Animator animator;
// Use this for initialization
protected Vector2 direction;
protected virtual void Start() {
animator = GetComponent<Animator>();
}
// Update is called once per frame
protected virtual void Update () {
Move();
}
public void Move()
{
transform.Translate(direction * speed * Time.deltaTime);
if(direction.x!=0 ||direction.y!=0)
{
AnimateMovement(direction);
}
else
{
animator.SetLayerWeight(1,0);
}
}
public void AnimateMovement(Vector2 direction)
{
animator.SetLayerWeight(1, 1);
animator.SetFloat("x", direction.x);
animator.SetFloat("y", direction.y);
}
}
然后说说血条和能量条
create-UI-image做出血条底+血条+能量条底+能量条
将stat分别赋予血条和能量条
stat的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class stat : MonoBehaviour {
private Image content;
[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;
}
}
// 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;
}
}
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;
}
}
}