Coderock

--------------------------------
link hover image

Unity 2D how to create a magnet

Frontpage calendar image January 1, 2017

Make your coin a Rigidbody and try velocit to the coin.  The idea is that you have a trigger circle around the coin. Also change the tag of the magnet gameobject to “magnet”. Then set the code inside the class of coin you want pulled in. bool inside; public Transform magnet; private CircleCollider2D circleCollider; void Start […]

link hover image

How to rotate object over time and oscillate

Frontpage calendar image November 29, 2016

I wanted to have an object rotate back and fourth between two specific angles on a single axis over time; it’s for a rocking 2D platform. public Vector3 from = new Vector3(0f, 0f, 135f); public Vector3 to = new Vector3(0f, 0f, 225f); public float speed = 1.0f; void Update() { float t = Mathf.PingPong(Time.time * […]

link hover image

Unity 2D How to hide a menu image in C#

Frontpage calendar image October 8, 2016

Get the reference to your specific button game object and set it to active or eactivate it using GameObject.SetActive(bool)

link hover image

Unity 2D dropdown selection in inspector

Frontpage calendar image October 6, 2016

public enum Car{ redneck,bike,mottor } public class SomeClass:MonoBehaviour{ public Car current; }

link hover image

Unity 2D Camera Zoom In Zoom Out

Frontpage calendar image October 5, 2016

Zoom Out: myCamera.orthographicSize = Mathf.MoveTowards(Camera.main.orthographicSize,18f,Time.deltaTime*2f);// 10; Zoom In: myCamera.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize,2f,Time.deltaTime*0.3f);//4;

link hover image

Unity 2D: How can I let something happen after a small delay?

Frontpage calendar image October 2, 2016

You can do it with coroutines in any MonoBehaviour: IEnumerator ExecuteAfterTime(float time) { yield return new WaitForSeconds(time); // Code to execute after the delay } And you can call it with, for example StartCoroutine(ExecuteAfterTime(5));

link hover image

Unity 2D countdown timer (Clock ) script

Frontpage calendar image September 17, 2016

Create an object hierarchy; Create a script and attach it to an object; Access namespaces; Use the Update method; Rotate stuff based on time. Add script on clock: And clockAnimator script content: public Transform hours; private const float hoursToDegrees = 360f / 60f; float timeLeft = 45.0f; // Use this for initialization void Start () […]