Skip to content

2D Stimulus Experiment

Markus edited this page Oct 2, 2016 · 5 revisions

How to build a simple 2D stimulus

Setup a 2D Project

  1. Create a new Unity Project and select 2D as project type! Do not Click Ok!
  2. Also select the 2D asset package using Add Asset Packages assistent.
  3. Click OK.

Setup your Stimulus

When the project has been loaded we do the following.

  1. Create a Sript with the Name Circle.cs with the following content.
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody2D), typeof(CircleCollider2D), typeof(SpriteRenderer))]
public class Circle : MonoBehaviour {

	[Range(0, 100)]
	public float bounceImpulseForce = 100;

	Rigidbody2D rb;

	void Start () {
		rb = GetComponent<Rigidbody2D>();
	}
	
	void OnCollisionEnter2D (Collision2D col)
	{
		var contact = col.contacts[0];

		Debug.DrawRay(contact.point, contact.normal, Color.green, 3f);

		rb.AddForce(contact.normal * bounceImpulseForce, ForceMode2D.Impulse);
	}
	
}

Now create a empty GameObject and add this script to it! You will see, that Unity does a few things for us. Adding some Components (the required ones!).

We can't see anything so we need a Sprite. Therefor, we using a graphic programm like Gimp, Inkscape or Photoshop to create a transparent picture with a white circle in it's center. Use the Size 512x512 and export the image as a png or bitmap to the Asset directory of our project.

Unity assumes this image already as a sprite so we can add this to the Sprite Renderer component by dragging the image to the Sprite property field.

Clone this wiki locally