Quantcast
Channel: Answers for "How to make objects falling down from the top of the scene?"
Viewing all articles
Browse latest Browse all 8

Answer by blueLED

0
0
This is not that hard a script as they're making it out to be, and it would benefit any beginner to find this solution. I know I would have wanted a simple answer like this when I started. Make your coin in Unity (create cylinder then scale it, etc.). Set your material and make it a prefab so you can easily update it later. Then attach a script to your coin. Call it "coinCollect". Open that script and paste this code in: using UnityEngine; using System.Collections; public class coinCollect : MonoBehaviour { public float fallSpeed = 8.0f; public float spinSpeed = 250.0f; void Update() { transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World); transform.Rotate(Vector3.forward, spinSpeed * Time.deltaTime); } void OnMouseDown() { renderer.enabled = false; } } Oh, and then don't forget to put your camera to where it can see the coin falling. Press play, done. Notes: "renderer.enabled = false" just makes the coin invisible when it's clicked. That way you can reuse the same coin by resetting the visibility after putting it back at the top of the screen when it hits a lower boundary (otherwise do a search on "Destroy" to remove the coin permanently if you don't want to recycle it). After that line, you could optionally play a sound effect for when the coin is clicked. Also, regarding the lower boundary, you should put a simple "if" statement in the Update function so that the coin doesn't just fall forever. Basically saying, "if the coin's y position is past the the lower limit, put the coin back to the top".

Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles



Latest Images