Related Posts Plugin for WordPress, Blogger...

5-11 Play Scale Animation When Getting Item

今天要介紹大家一個簡單的功能,就是當玩家獲取物品時,背包內的Item會有簡單的縮放動畫,這個設計是讓玩家能較清楚的看出取得物品的變化。

如下圖,剛取得物品的時候會較大。

然後物品會再慢慢縮減到正常大小。

這個功能不難,我們主要修改ItemUI.cs:

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

namespace RPG.Inventory{
 public class ItemUI : MonoBehaviour{
  public Item Item { get; set; }
  public int Amount { get; set; }

  [SerializeField] float smoothScaleMultiplier = 4f;
  float targetScale = 1f;
  Vector3 initialScale = new Vector3(1.4f, 1.4f, 1.4f);

  void Update(){
   // 獲取物品動畫
   if (transform.localScale.x != targetScale) {
    float scale = Mathf.Lerp (transform.localScale.x, targetScale, smoothScaleMultiplier*Time.deltaTime);
    transform.localScale = new Vector3 (scale, scale, scale);
    if (Mathf.Abs (transform.localScale.x - targetScale) < 0.01f) {
     transform.localScale = new Vector3 (targetScale, targetScale, targetScale);
    }
   }
  }

  public void SetItem(Item item, int amount = 1){
   transform.localScale = initialScale; // 獲取物品動畫的初始Scale
   this.Item = item;
   this.Amount = amount;
   GetComponentInChildren ().sprite = Resources.Load (item.Sprite);
   GetComponentInChildren ().text = Amount.ToString();
  }

  public void AddAmount(int amount = 1){
   transform.localScale = initialScale; // 獲取物品動畫的初始Scale
   this.Amount += amount;
   GetComponentInChildren ().text = Amount.ToString();
  }

  public void SetAmount(int amount){
   transform.localScale = initialScale; // 獲取物品動畫的初始Scale
   this.Amount = amount;
   GetComponentInChildren ().text = Amount.ToString();
  }

  public void Show(){
   gameObject.SetActive (true);
  }

  public void Hide(){
   gameObject.SetActive (false);
  }

  public void SetLocalPosition(Vector3 localPosition){
   transform.localPosition = localPosition;
  }
 }
}


然後,大家可以修改InventorySystem.cs,在Update中增加GetKeyDown事件,按下G鍵的時候會隨機產生一個物體,能夠方便大家觀察動畫的模樣。

留言