Coding Challenges

Updated on April 28, 2017 in Challenges
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
10 on May 13, 2015

Hello everyone,

now and then I see people asking for something to program, or something to learn.
So to help I have written down some exercises, or challenges, for you to do.
If you have a solution for one of the challenges, please post it below!

If you get stuck, be sure to ask about it here, or in a separate thread. Also, if you have a suggestion for a challenge, I’d like to hear those too!

I will try to sort the challenges a bit on difficulty.
These challenges are intended for C# and unity, but should be do-able in other languages as wel 😀

Beginner:
– Change an object’s color when you click on it.
– Create an object spawner that spawns a prefab every second.
– Create a cube that you can ‘push’ away when you click on it. (physics)
– Controll a camera’s FOV with your scrollwheel.

Advanced:
– Create 2 threads. Make them log the numbers 0 – 99 to the console at the same time.
– Create a generic method. Log the datatype to the console of the variable that you feed it.
– Create an extension method with wich you can reset a transform’s position and rotation to 0, 0, 0.

Suggested:
– Create canon which fires rigidbody this. and precalulate the hit position of current cannon angle and the type of selected this.(mass,drag etc); (by Eliman)
– Create physics bullet, that can penetrate obstacles, and loses its inertia depending of that obstacle width; (by Eliman)
– Calculate point in space where two object will intersect depending on their movement trajectory (by Eliman)

Good luck, and let me know of your progression!

  • Liked by
  • maxartz15
  • MistaPoplop
  • Mrbelbala
  • Spangui
  • Eliman
  • +1
Reply
2 on May 14, 2015

Beginner:
– Change an object’s color when you click on it.

using UnityEngine;
public class ColorChange : MonoBehaviour {
 void OnMouseDown()
 {
 GetComponent<MeshRenderer>().material.color = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
 }
}

– Create an object spawner that spawns a prefab every second.

using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
 [SerializeField]
 GameObject prefab;
 void Start () {
 StartCoroutine(Timer());
 }
 IEnumerator Timer()
 {
 while (true)
 {
 Instantiate(prefab, transform.position, transform.rotation);
 yield return new WaitForSeconds(1);
 }
 }
}

– Create a cube that you can ‘push’ away when you click on it. (physics)

using UnityEngine;
public class PushAway : MonoBehaviour {
 Rigidbody rb;
 void Awake () {
 rb = GetComponent<Rigidbody>();
 }
 void OnMouseDown()
 {
 rb.AddForceAtPosition(Camera.main.transform.forward * 200, transform.position);
 }
}

– Controll a camera’s FOV with your scrollwheel.

using UnityEngine;
public class FovChange : MonoBehaviour
{
 void Update()
 {
 if (Input.GetAxis("Mouse ScrollWheel") > 0) // IN
 {
 Camera.main.fov -= 5;
 Camera.main.fov = Mathf.Clamp(Camera.main.fov, 10, 120);
 }
 if (Input.GetAxis("Mouse ScrollWheel") < 0) // OUT
 {
 Camera.main.fov += 5;
 Camera.main.fov = Mathf.Clamp(Camera.main.fov, 10, 120);
 }
 }
}

on April 28, 2017

Is this JavaScript?

Guru
on April 28, 2017

No, C#.
Though JS looks a lot alike.

Show more replies
  • Liked by
Reply
Cancel
2 on May 14, 2015

Advanced:
– Create 2 threads. Make them log the numbers 0 – 99 to the console at the same time.
(WARNING)since in my example i dont have any shared data i didnt syncronize them

using UnityEngine;
using System;
using System.Threading;
public class Threading : MonoBehaviour {
 void Start () {
 Thread t1 = new Thread(new ThreadStart(Printer));
 Thread t2 = new Thread(new ThreadStart(Printer));
 t1.Start();
 t2.Start();
 }
 public static void Printer()
 {
 for (int i = 0; i < 99; i++)
 {
 Debug.Log("in Thread: " + i);
 Thread.Sleep(50);
 }
 }
}

– Create a generic method. Log the datatype to the console of the variable that you feed it.

using UnityEngine;
using System.Collections;
public class TypePrinter : MonoBehaviour {
 void Start()
 {
 LogMe<int>();
 LogMe<float>();
 LogMe<string>();
 LogMe<decimal>();
 LogMe<TypePrinter>();
 LogMe<short>();
 LogMe<ColorChange>();
 }
 void LogMe<T>()
 {
 Debug.Log(typeof(T).ToString());
 }
}


– Create an extension method with wich you can reset a transform’s position and rotation to 0, 0, 0.
using UnityEngine;
using System.Collections;
public static class GameObjectExtension {
 public static void Reset(this GameObject go)
 {
 go.transform.position = Vector3.zero;
 }
}
//test example
public class ResetPos : MonoBehaviour { 
 void Start()
 {
 gameObject.Reset();
 }
}

Guru
on May 14, 2015

Nice work!
But I think you posted the wrong post for the generic method :)

Wise
on May 14, 2015

fixed. Can`t get rid of  <em> </em>  tag tho =(

Show more replies
  • Liked by
Reply
Cancel
3 on May 14, 2015

My suggestion to this topic : add more practical tasks, more Math oriented stuff,
for example:
-create canon which fires rigidbody this. and precalulate the hit position of current cannon angle and the type of selected this.(mass,drag etc);
-create physics bullet, that can penetrate obstacles, and loses its inertia depending of that obstacle width;
-calculate point in space where two object will intersect depending on their movement trajectory

Guru
on May 14, 2015

Your suggestions are also good!

But my challenges are practical:
I try to use generics when I can.
I use a lot of threading.
And extensions can be very nice!

What I tried to do is getting people in contact with new stuff to learn.

Wise
on May 14, 2015

Can u give example where u use threading a lot? From my experience unity build their engine to avoid using threads, and stimulate programmers to avoid those stuff also.. maybe its just me who worked for two years with microcontroller where no threading is supported ;C

Guru
on May 14, 2015

I’m working on a multiplayer game. So all networking logic is using two main seperate threads and some worker threads. My AI will probably work on a seperate thead too.

But no, if you don’t want to, you don’t have to use threads in Unity using coroutines. (Still you can get better performance if you do use threads)

Show more replies
  • Liked by
Reply
Cancel