This is a 3-week-old assignment that I didn’t manage to do until now. The goal is to create a VR painting program.
Everytime I start to code I just freeze looking at the empty script. I just don’t know how to organize my thoughts and don’t have a clear idea of what is to be achieved. For this reason, I decided to write everything I want to do in plain English.
Before coding I set up the scene using the basic SteamVr package. I set up a Teleport area for locomotion; a plane to make this area visible; a cube platform to set my objects and for orientation; three cubes prefabs, each with a different material/color (red, green and blue) and finally a SteamVR player controller. I also put some throwable objects just to test the controller responsiveness.
The class template has an empty object called “InteractiveObject” where the BrushHandler and ColorSelector scripts are kept. The ControllerHandler script is found in the Players “RighHand”.
Now for the coding part:
· Have a Brush object
· The brush is controlled with the VR controller.
· Have colored objects on the scene that will serve as colorPalette.
· When Brush touches one of these objects – it changes to the objects color.
· When pressing the Trigger Button on the controller, the Brush creates many copies of itself named paintObjects, allowing to paint the scene with it.
· The paintObject must have the color of the brush.
· If the brush touches a colorPalette with a different color, it should change color but the paintObject already on the scene should remain with its original color – Once paint is set, it should remain the same color. (no changing the color of what was already drawn)
· There should be a limit on how many paintObjects are on the scene at any given time: a limit on how much paint can be on the scene. If this limit is reached, the ‘oldest’ paintObject must be deleted.
There are three scripts already created by the class instructor Igal Nassima. They come with comments and guidelines, but I don’t really understand his workflow. I’m not sure where Igal want us to set the scripts; if the Brush object is created by the script or if it will already be on screen.
I will try to do it somehow…
o Makes the brush paint
o Checks what color is in the brush
o Get the brush color
o Creates instances of the paintObject that has the current color of the brush.
o “Create a public Color Type that can be set from the inspector to be used in each menu Item” – (comment by Igal) – Don’t understand what he means by “color type” and why it must be set from the inspector.
o “Setup Collider Trigger to send the color of this object to the Brush Handler” – Checks if there is a collision between the brush and another object and send the color of this other object back to the Brush Handler.
o “Find Object of Type à then call the function that sets the currentColor” –
o Store the current color on the brush
o Sets what gameobject will be drawn/created/instantiated when activated by the trigger on the controllerHandler.
o //create a public function that SETS UP the color of the currentColor (hint, function must have a Color variable)
o //create a public function that RETURNS the Color of the currentColor
§ Don’t know the difference between Set UP the color and “Returns the Color” and what it is supposed to do..
§ Also, Don’t know how to make scripts communicate with one another.
Found an example at my colleague Ellen Nickles documentation of this assignment:
//Inside the ControllerHandler script it calls the the color on BrushHandler:
private void OnTriggerEnter(Collider other)
FindObjectOfType<BrushHandler>(). currentColor = myColor;
Creating the Brush
The Brush doesn’t paint, but stores its own color and changes color when
· The brush is a Sphere prefab named ‘Brush’.
· It will have a color.
· It will get the color of the object it touches
· It will return the color it has
· It has a script called BrushHandler. It will set the following behaviors:
o Stores the currentColor
o Creates/Returns the prefab that will be drawn (the “paint”)
Starting to write the code itself
Don’t really know where to start. I look at the three template scripts, but don’t know which function to make. I don’t even know if I should create the brush object of if it is instantiated by one of the scripts.
In order to practice basic coding, I also created a Test script to try things out.
I created a transform function that makes a cube move and it asks for a GameObject. I added a different object on the inspector but the object where the script was attached – the cube - still was the one that moved. Why?
1 Creating – Instantiating an object
I will create a brush and make it instantiate copies of itself when user press the Oculus Controller Trigger button. I’ll figure the other actions (changing color) later.
We managed to run a basic instantiate code copied from the Unity API manual. At first it didn’t worked, because we added the script to the prefab we wanted to instantiate. This freezes Unity because it was trying to instantiate the object WITH the instantiate script. Once we added a different prefab to the public GameObject slot in the Inspector the instantiate code worked fine.
Basic Unity Instantiate example 1:
//The Manual didn’t used “public”.
public Transform prefab;
for (int i = 0; i < 30; i++)
Instantiate(prefab, new Vector3(i * 2.0F,i* 1, 0), Quaternion.identity);
Basic Unity Instantiate example 2:
public class TestScript : MonoBehaviour
// Instantiate a rigidbody then set the velocity
public Rigidbody projectile;
// Ctrl was pressed, launch a projectile
// Instantiate the projectile at the position and rotation of this transform
clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
STARTING FROM SCRATCH (?!!)
For some reason it seems that SteamVr plugin is no longer functional on my project. I can’t find the Steam Controller Input inside the ‘Window’ menu. I downloaded the template project again and it seems to be working fine there. I’ll have to setup my scene again.
Update: Maybe the SteamVR plugin wasn’t turned on (for some reason). I started a new project anyway.