Terrain Clicking Creation in More Detail w/ Video
Here is what I learned about Instantiate yesterday:
- You can use a prefab, think of it like a stamp, and use it with the Instantiate function to create objects.
- The script needs a reference to the prefab. Right now all I know it that means selecting the prefab in the IDE from exposed object variable in my script. (This will need to be fixed because I need to be able to set this in code as players pick different objects to place.)
Number 2 is the thing I was stuck on that made my code not work for awhile. So yeah pretty much you can blame me not knowing what I am doing yet.
Here is the code I have on the terrain to create an object. I am thinking this will eventually not be on the terrain because what I really want is a copy of the prefab following the mouse till you click to place it. The Terrain click would send an event to that prefab to place on terrain where it is at.
var SampleObject:GameObject;
function OnMouseUp(){
var pos = Camera.main.GetComponent("TerrainLine");
var SampleObject =
Instantiate(SampleObject,
Vector3(pos.x_pos, pos.y_pos, pos.z_pos),
Quaternion.identity);
}
The Camera.main.GetComponent is used to access the script in the camera that is tracking the location on the terrain. (This is the same script making the terrain highlight.)
Quaternion.identity is completely and shamelessly stolen from a sample script and I am not sure what it means yet.
Here is a video of what the click to place does right now and some basic panning using the arrow or WASD keys.
Click to place objects working!
I will do an in detail post later, but a quick note. I figured out my problem with creating objects where the user clicked the terrain! It was actually pretty simple and had a lot to do with me just not knowing what Unity was expecting. As part of this test I was also able to do a test import from Blender which worked okay. I am a bit annoyed it didn’t import with textures, but whatever, you get what you pay for.
First Unity3D Wall
Okay so I thought no problem I will just create a new object where I clicked on the terrain. Sounds like it would be easy right? Well after about an hour of looking and digging and error codes I still didn’t figure it out.
It is just so much fun learning a new game maker when I used to know one so well I could make things in my hour lunch. Sigh…
Will it stop me? Not a chance.. back to it tonight if I have the time!
Side note. I have a 45degree camera that pans on keyboard left, right, up, down, or WASD
That part was real easy so the night wasn’t a total loss.
Well maybe not?
Okay, so you saw the demo and it looked neat, after getting that working and looking at it, I have pretty much decided I will not use that technique to highlight the ground. What I will do though it create an object that floats over the ground so it can be placed right where it looks like it will be placed. Totally not wasted work, but the idea once seen made me think it wasn’t the best way to deal with the idea I have.
Ok personal internet time up for this break. To be continued.
Working Projector onto terrain demo.
http://www.youtube.com/watch?v=eFxanWGmBc4
Here is a video of a basic projector following the mouse, I know the quality is questionable but you make due with a budget of zero.
The projector is attached to a GameObject that changes position to match the current position of the raycast hit onto the terrain. Seems to work.
Few tricks, I had to find a additive shader example to highlight the terrain instead of darken it. The texture had to have solid black edges or strange things would happen. The materials had to be set to clamped. I know I may be forgetting something else I set, but for now that is that.
Highlight the terrain.. easy right?
function Update(){var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (collider.Raycast (ray, hit, 100.0)) { Debug.DrawLine (ray.origin, hit.point); } }
So remember this code. Here is what I have found out so far. It actually does work, but doesn’t draw a line like I was expecting, well not one I can see. I think what is happening is the line is projecting straight into the camera which makes it one pixel when looked at from the camera it is projected from, kinda like holding a pencil so you can only see the eraser. Anyway I figured out this script needed to be attached to the Camera to make it work, there was no mention of that in the code sample. I am sure they just figured everyone would know that.
As I worked on it I also learned how the print() function works to place the values I wanted to inspect into the console. So for now I am projecting a ray down to the terrain and returning the point on the terrain I am hitting.
Now what I need to do is take that point and create an object that doesn’t render above it and attach a projector to that object that is pointed down. Add the right material to that projector and I should have highlighted a spot on the ground where the mouse is pointing. Step one at allowing a player to place an object on the ground. Yay for progress; even if it is slow!
How to make a line on a point in the terrain?
I want to know where on the terrain the mouse is pointing so I can make the projector point down from that spot onto the ground. Without coding anything, (since I do this research at work and can’t install Unity without a swift write up) I have found the following code.
function Update(){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (collider.Raycast (ray, hit, 100.0)) { Debug.DrawLine (ray.origin, hit.point); }
}
It seems like this will do the trick.. Obviously this code isn’t manipulating a camera, but it looks like a good starting point. I do need to figure out how to have the ray pass through objects and hit the terrain below, but I think I may have found a good link for that too.
I sure wish after programming all day at work I had more motivation to program more at home. I really need to start making some test code and seeing where the technical limits are hiding.
My it has been awhile!
In an effort to keep me motivated and making progress on eventually gaining employment as a Game Developer I will be posting my progress as I make my way through Unity 3D and hopefully come out the other side with a working demo, or even a Facebook game in Unity 3D.
Yesterday I spent a bit of time at lunch digging for how I would place a highlighted spot on the terrain below the mouse pointer and wasn’t successful, but I need to tinker some with the possible methods before I give up. It is looking like my best bet might be something called a projector. I just need to figure out if I can attach it to something that tracks the location of the mouse and have it always point directly down to the ground. I think that might accomplish what I want.
What that will allow is the click of the mouse will place an object at the location of the highlight on the terrain. Which of course is another thing I need to figure out. Oh and all of this will be for placement of object by a player or do actions on that spot on the terrain.
What am I thinking for a game? Something farm like and island like, but with a twist that allows a bit more twitch gameplay if the player wants to activate that piece of the game. Will I finish something like this? Absolutely no idea right now, but I would like to as I am really excited about the design I have on paper right now.
My biggest obstacle besides time will be asset creation. I will either have to make some really rough mockups in Blender or find someone that can do 3D modeling later when I want to polish the graphics.
I also have no plans to make the whole game at once. I want to make the semi twitch game first then the farm island game then combine them then integrate into Facebook if it still seems fun. So really I am making three games and it any one piece isn’t fun I can just dump it and release the single game.
Oh and along these lines I have created a logo for my games:


