- Home /
How to show the effect when the coins are collected moves to the coin meter?
I would like to know how to create the effect which is in the temple run game in whihc when the coins are collected, the coins flies towards the coin meter. Is it just using a texture or using a 3d model for this purpose? Can some one please help me out? In my game, which is an infinite runner type game, the player collects coins and it is showing in the coin meter. But I want to show that the coins collected are moving to the coin meter to the player. How can it be shown? Some one please guide me regarding this topic. Thanks in advance.
Hope this will help, I think this is the exact thing you are asking for: https://www.youtube.com/watch?v=GgNLr7SSh1I&t=107s
Answer by blueLED · Dec 12, 2014 at 08:48 AM
Make a coin-shaped cylinder and attach this script to it:
using UnityEngine;
using System.Collections;
public class flyToMeter : MonoBehaviour {
public GameObject meter;
void Update () {
transform.position = Vector3.Lerp(transform.position, meter.transform.position, 1.5f * Time.deltaTime);
}
}
Then, in your scene, make a cube, or empty game object and position it roughly where the meter is and then set that as the "meter" in the coin's inspector. Press play, the coin will fly to the meter.
This works for getting a nice eased-out movement effect, although I cringe every time I see Time.deltaTime as the parameter in an iterative Lerp movement :)
Is Time.deltaTime not the right way? I thought that was SOP.
Lerp(A, B, t) basically moves from A towards B by t percent.
If you assume your framerate is 10 (deltaTime 0.1) you are always moving from current position to target position by 10%. So first update takes you to 90%, next 81% next 73% ... You will never get to exactly B because the distance gets basically multiplied by 0.9 every Update(), but you will make a nice easing effect where the object moves faster in the beginning and slows down as it gets nearer to target.
You should use deltaTime to get smooth movement, but in a way where you make sure that it hits 1 at some point. Typically this would mean something like storing the start and end points of the intended Lerp when the movement starts, then incrementing a public float incrementingTime
in Update by deltaTime, and then Lerping with that until incrementingTime
hits 1:
transform.position = Vector3.Lerp(originalStartVector3, targetV3, incrementingTime);
But that's totally outside the scope of this question :D and what you proposed actually makes for a nicer look an feel for the game the OP is making.
Ins$$anonymous$$d of moving it from coins vector 3 position, I want it to be moved from the players transform.position.
Answer by IcyHammer · Dec 11, 2014 at 04:08 PM
The easiest way to do it, is to calculate the point of your coin meter label in world space by using Camera.ScreenToWorldPoint. Then you can animate you coin for example by rotating it around its Y axis and at the same time scaling it down to appropriate size and moving it towards that calculated point.
So for that purpose would I need a 3d model of the coin or just the texture image of the coin.
Yes, in order for this method to work, you would need a 3d model of a coin and a texture.
Answer by sundhar_unity · Aug 19, 2020 at 05:05 PM
Hope this will help, I think this is the exact thing you are asking for: https://www.youtube.com/watch?v=GgNLr7SSh1I&t=107s