Hello everyone, i’m new to the forums, and Unity, and I just had a couple questions about scripting. I’m mainly a 3D artist, but i’m familiar with some C++, and I’ve done some platform game tutorials in Flash, so I’m confident I can learn Java, I just need a good starting point. I understand the syntax, and how components all control gameobjects, but i’m unsure as to the scope of what i can do in script. I’ve read the PDF scripting tut on the main site, and it touched on a few good functions in section 9 - Common Script Types, i’m looking for something like that; sort of a list of common functions for game events. I’ve done the 2D and 3D platform tuts as well, and they don’t teach too much about scripting, they just give them to you and tell you where to plug 'em in. Any help is greatly appreciated!!
Movement, collision and event management, camera manipulation, game state management, appearance modification, physics, and networking events are the things I do.
I love Javascript scripting, and I’ve been using Unity for close to 2 years. My projects tend to be code-heavy cuz it’s so fun. What specific things are you interested in accomplishing through script?
Sweet, maybe you have some tips for me? I don’t expect complete hand-holding but a hard shove in the right direction would be much appreciated! I wanna make one of those little games where you rotate the level to navigate a ball around while avoiding holes,
so im looking at locking the mouse cursor in the center of the screen (so you don’t click outside the window), rotating the level based on the speed/direction of the mouse, handling the rigidbody physics, and setting up the gui. Perhaps if you’re interested we could horse-trade some tutoring for some 3D work?
No horse-trading required … though I don’t have a bunch of time either. But you’ve broken it up into pieces pretty nicely:
-
set up the board
-
freeze (or hide?) the mouse pointer
-
rotate the board based on actual mouse position
-
make a GUI
-
keep track of the ball position and see if it falls through a hole
-
that’s all you
-
not sure about this one, but someone on the forums will!
-
I just did something like this. Code similar to the following might do the trick, with some minor tweaking:
public var go_board : GameObject; //put the board into this variable in the Inspector
private var fl_screen_horiz_middle : float;
private var fl_screen_vert_middle : float;
function Start() {
//set up a couple variables
fl_screen_horiz_middle = Screen.width / 2;
fl_screen_vert_middle = Screen.height / 2;
}
function Update() {
//tip the board based on mouse position
go_board.transform.rotation.eulerAngles.z = (Input.mousePosition.x - fl_screen_horiz_middle) / fl_screen_horiz_middle;
go_board.transform.rotation.eulerAngles.x = (Input.mousePosition.y - fl_screen_horiz_middle) / fl_screen_horiz_middle;
}
That’s TOTALLY UNTESTED but it’s the basic concept. Of course you can tip the board more if you multiply the rotations (in function Update) by some value. As the code is, it will only rotate by one degree.
-
I’ve done GUIs but there are a lot of pieces that you can use. Find a GUI tutorial on the Unity site.
-
To find if the ball fell through a hole, create an empty game object, put a box collider on it (or some other shape of collider), and select its “Trigger” checkbox. Then place that under each hole in the board. Make sure it is a child of the board game object, so it tilts along with it. To code it, check out the code sample at Unity - Scripting API: MonoBehaviour.OnTriggerEnter(Collider) . OnTriggerEnter a pretty simple event that just tells you if anything entered the trigger. Since the ball is the only thing moving relative to the board, you can be sure it’s the ball bumping the trigger, and you can handle it accordingly – teleport it back to the starting point, subtract 1 from the number of balls, etc.
Hope this helps!
Thanks Brett, it’s definitely a start, and i appreciate it tons, but do you know of any script resources or pages that have like a compiled list of valid functions, or ways to manipulate gameobjects? Like for example, i see that you rotate the game board based off of how far the mouse cursor is from the center of the screen, and i understand HOW the
go_board.transform.rotation.eulerAngles.x
code works, but i didn’t know you had to specify Euler or Quaternion rotation. How did you go about learning Unity thru java? I don’t want to have to ask the forums thru every part of my game, ya know?
Scripting was mainly trial and error and a lot of time searching the Unity Script Reference at Unity - Scripting API: . Plus a lot of time here on the forums, asking questions and looking at old posts! I still use the Script Reference several times a day on a typical day – like “what kind of parameter does OnTrigger pass?” or “What are all the Hashtable variables and functions again?” There’s SOOO much to know.
So I recommend doing what you’re doing: break up the project into component parts. Tackle them one at a time. Learn by doing. (That’s the only way I can motivate myself to do research – if I need the answer to solve an immediate problem!)
And people on the forum are really generous with their time. Soon you’ll be passing on YOUR experience to newbies here!
That’s pretty much what I figured… I’m going to move my question relating to this game board rotation to a new thread if you still feel like offering your insight. Thanks again!