Unity uses AnimationCurve to achieve the dynamic effects of ScoreFlash and unitycurve in RPG attack and injury scores
Sun Guangdong: 2015-2-8/2: 19 reprint please indicate the source: http://blog.csdn.net/u010019717
More comprehensive content please see my game pretty cool address: http://www.unitymanual.com/space-uid-18602.html
Let's take a look at the common implementation methods:
Name a javascript script. Copy the following code and then link it to an object to observe the effect.
Var Point: float; // The private var GetHitEffect: float; private var targY: float; // the position where the score is to be displayed. The Y value is private var PointPosition: Vector3; // The position where the score is displayed. var PointSkin: GUISkin; var PointSkinShadow: GUISkin; function Start () {Point = Mathf. round (Random. range (Point/2, Point * 2); PointPosition = transform. position + Vector3 (Random. range (-1, 1), 0, Random. range (-1, 1); targY = Screen. height/2;} function OnGUI () {var screenPos2: Vector3 = Camera. main. camera. worldToScreenPoint (PointPosition); GetHitEffect + = Time. deltaTime * 30; GUI. color = new Color (1.0f, 1.0f, 1.0f, 1.0f-(GetHitEffect-50)/7); // the opacity of the score gradually disappears. skin = PointSkinShadow; GUI. label (Rect (screenPos2.x + 8, targY-2, 80, 70), "+" + Point. toString (); GUI. skin = PointSkin; GUI. label (Rect (screenPos2.x + 10, targY, 120,120), "+" + Point. toString ();} function Update () {// move the score up targY-= Time. deltaTime * 200 ;}
Then we will introduce the curve motion.
Create a script as follows:
// Configure // <copyright file = "MyTween. cs "company =" Game Developer "> // <author = Sunguangdong> /// </copyright> /// <summary> // It can be mounted on the UI, when kill is hit, the score is dynamically displayed // </summary> // specify using UnityEngine; using System. collections; using System; public class M YTween: sums {public bool counts = false; public bool PosSpeedCurve = false; public AnimationCurve PosCurve; public Vector3 m_BeginPos = Vector3.zero; public Vector3 m_EndPos = Vector3.zero; public bool UseTweenScale = false; public AnimationCurve ScaleCurve; public Vector3 m_BeginScale = Vector3.one; public Vector3 m_EndScale = Vector3.one; public B Ool UseTweenRot = false; public AnimationCurve RotCurve; public Vector3 m_BeginRot = Vector3.zero; public Vector3 m_EndRot = Vector3.zero; /// <summary> /// duration of tween /// </summary> public float TweenTime = 1f; // public void Play (Vector3 offset, bool reverse = false, Transform target = null, Action callback = null) {m_BeginTime = Time. time; m_Reverse = reverse; m_Target = target; m_EndCa Llback = callback; m_Offset = offset; m_PlayTween = true;} // <summary> // start time of tween /// </summary> private float m_BeginTime = 0f; private Vector3 PosDist {get {return ToPos-FromPos;} private Vector3 ScaleDist {get {return ToScale-FromScale ;}} private Vector3 RotDist {get {return ToRot-FromRot ;}} private Vector3 UILocalPos {get {return m_Target = null? Vector3.zero: m_Offset + UITool. PosToLocal (m_Target.position, m_RootScale);} private Vector3 FromPos {get {return m_Reverse? UILocalPos + m_EndPos: UILocalPos + m_BeginPos;} private Vector3 ToPos {get {return m_Reverse? UILocalPos + m_BeginPos: UILocalPos + m_EndPos;} private Vector3 FromScale {get {return m_Reverse? M_EndScale: m_BeginScale;} private Vector3 ToScale {get {return m_Reverse? M_BeginScale: m_EndScale;} private Vector3 FromRot {get {return m_Reverse? M_EndRot: m_BeginRot;} private Vector3 ToRot {get {return m_Reverse? M_BeginRot: m_EndRot;} private float EndTime {get {return m_BeginTime + TweenTime;} private float PosVal {get {return m_Reverse? 1-PosCurve. Evaluate (CurTime): PosCurve. Evaluate (CurTime) ;}} private float ScaleVal {get {return m_Reverse? 1-ScaleCurve. Evaluate (CurTime): ScaleCurve. Evaluate (CurTime) ;}} private float RotVal {get {return m_Reverse? 1-RotCurve. Evaluate (CurTime): RotCurve. Evaluate (CurTime);} private float CurTime {get {return m_Reverse? (EndTime-Time. time)/TweenTime: (Time. time-m_BeginTime)/TweenTime;} private void UpdatePos () {if (! UseTweenPos | PosCurve = null) {return;} if (PosSpeedCurve) {transform. localPosition = FromPos + PosDist * PosVal;} else {m_CurVect.x = FromPos. x + PosDist. x * (Time. time-m_BeginTime)/TweenTime; m_CurVect.y = FromPos. y + PosDist. y * PosVal; m_CurVect.z = FromPos. z + PosDist. z * (Time. time-m_BeginTime)/TweenTime; transform. localPosition = m_CurVect;} private void UpdateScale (){ If (! UseTweenScale | ScaleCurve = null) {return;} transform. localScale = FromScale + ScaleDist * ScaleVal;} private void UpdateRot () {if (! UseTweenRot | RotCurve = null) {return;} transform. localRotation = Quaternion. euler (FromRot + RotDist * RotVal);} void Start () {m_BeginTime = Time. time; m_RootScale = UIRoot. findObjectOfType <UIRoot> (). transform. localScale;} void Update () {if (m_PlayTween) {if (Time. time> EndTime) {m_PlayTween = false; if (m_Pingpong) {m_Reverse =! M_Reverse; m_PlayTween = true; m_BeginTime = Time. time;} else {if (m_EndCallback! = Null) {m_EndCallback () ;}return ;}updatepos (); UpdateRot (); UpdateScale ();}}}Note: The program is for NGUI
Set the curve as follows:
The effect cannot be seen from the image. Do it yourself!