ΠΓΣ 518: ΧΕΔΙΑΜΟ ΗΛΕΚΣΡΟΝΙΚΩΝ ΠΑΙΧΝΙΔΙΩΝ Θέμα: Βασικά για Scripting Tutorial 3: Attack the wall 1. Create a new project 2. Import Packages: When you complete your prototyping environment will feature a floor comprised of a cube primitive, a main camera through which to view the 3D world, a Point Light. It will look something like this: - 1 -
SETUP THE SCENE 3. Create a Cube: Name: Floor Position (0, 0, 0) (YOU CAN DO THAT ALSO USING Reset Position option see image below) Scale (100, 1, 100) 4. Create a Point Light: Position (0, 20, 0) Range 40 ANOTHER BRICK IN THE WALL Let s make a wall of bricks. We ll do this by creating a single master brick and duplicate it Building the master brick 5. Create a Cube Position (0,1,0) Focus on your object: Select the Cube in the Hierarchy. Hovering your mouse in the Scene view and press F. 6. Add physics to your Cube Component Physics Ridigbody (from the top menu) 7. Color the Cube by creating a Material: Create a Material (Project view) Rename it Red Inspector Click the color block of Main Color Choose a red color - 2 -
Apply the material by dragging this material Red on the Cube Now our cube is ready to be duplicated but let s have a quick look at the physics in action 8. Select Cube Position Y = 15 Rotation X = 40 Press PLAY Set back the values: Position Y = 1 Rotation X = 0 Snap To help position your objects, Unity allows you to snap to specific increments when dragging these increments can be redefined by going to Edit Snap Objects To use snapping: hold down Command(MAC)/Control(PC) when using the translate tool (W) 9. In order to start building the wall duplicate the Cube: Command/Control + D Snap-move your cube one unit in the X axis, shown as follows: Repeat this procedure of duplication and snap-dragging until you have 10 cubes in a row/line - 3 -
Grouping and duplicating with empty objects 10. Create an empty object: GameObject Create Empty from the top menu Name: CubeHolder Position (4.5, 0.5, -1) 11. Drag the list of 10 Cubes in the CubeHolder (in the Hierarchy) in order to make the CubeHolder the parent of the 10 Cubes 12. Duplicate the parent object (CubeHolder) and use Snap-dragging as before to lift by 1 unit in the Y axis Repeat the procedure to create 8 rows of bricks - 4 -
KNOCK IT DOWN! Now that we have build the wall, let s make a simple game mechanic, where the user can maneuver the camera and shoot the bricks at the wall and knock it down 13. Setting the camera facing the wall. Select the Main Camera Position (4, 3, -15) Rotation (0, 0, 0) WALL ATTACK! 14. Create a new Javascript script Name: Shooter Script: var bullet : Rigidbody; var power : float = 1500.0; var movespeed : float = 5.0; function Update(){ Remember to save your script! 15. Attach Shooter script on the Main Camera Do this by dragging the script (Project view) on the Main Camera object (Hierarchy) Moving the camera We want to use the arrow keys in the keyboard so we need to be aware of how to address them in the code. Unity has many inputs that can be viewed and adjusted using the Input Manager. Edit Project Settings Input - 5 -
Two of the setting for Input are Horizontal and Vertical. These rely on an axis based input that when holding the Positive Button builds to a value of 1, and when he hodling the Negative Button builds to a value of -1. Example in the script: Input.GetAxis( Horizontal This receives the current value of the horizontal keys a value between -1 and 1 depending upon what the user is pressing. 16. Change the Update function of the Shooter Script to the code below function Update () { var h : float = Input.GetAxis("Horizontal") * Time.deltaTime * movespeed; var v : float = Input.GetAxis("Vertical") * Time.deltaTime * movespeed; Explanation: - 6 -
Time.deltaTime is the time it takes from one frame to the next one we use this to have a consistent amount per second regardless the framerate!! Ανεξαρτιτωσ αν είναι διαφορετικόσ ο ρυκμόσ απεικόνιςθσ του κάκε frame (λόγω πολλϊν ι λίγων υπολογιςμϊν) να γίνεται με τον ίδιο τρόπο/ρυκμό θ αλλαγι τθσ μεταβλθτισ. 17. Translate the camera: add the bold line of the code in the Shooter Script function Update () { var h : float = Input.GetAxis("Horizontal") *Time.deltaTime * movespeed; var v : float = Input.GetAxis("Vertical") * Time.deltaTime * movespeed; transform.translate (h, v, 0); With this extra line of code we say to the Unity to change the Translate variable of Component Transform (of the object that runs the current script) with values (h, v, 0) for x, y, z axis respectively. Remember we attached this script (Shooter) on the MainCamera so this will affect the MainCamera BE CAREFUL WITH CASE OF THE LETTERS IMPORTANT!!!!! Save your Script (from MonoDevelop editor) Save your Scene (from Unity) Remember both in the Assets folder Making a bullet/projectile (ςφαίρα όπλου) Creating the projectile prefab 18. Create a Sphere Select the Sphere and note at the inspector the 3 components: Mesh Filter: This is to handle the shape Renderer: This is to handle the appearance Collider: This is to manage interactions (collisions) with other objects 19. Create a Material, blue color, name the new material as bulletcolor and apply this color on the Sphere you have just created 20. Add physics - 7 -
Select the Sphere Bullet and add a Rigidbody component Component Physics Rigidbody As we wish to fire this bullet/projectile (ςφαίρα όπλου) when the player presses a key, we do not want the bullet to be in the scene from the beginning, but instead we want to be stored and created when the key is pressed. For this reason we will store the object as a prefab and use our script to instantiate (that is, create an instance of) it, only when a key is pressed. 21. Storing with prefabs: Click Create at the Project panel Choose Folder, then rename it Prefabs Drag the Bullet object (from Hieararchy) into Prefabs folder (Project panel) Rename this new prefab form Sphere to Projectile Delete the object Sphere from the Hierarchy Note that dragging a GameObject into the Project panel will save it as a prefab-there is no need to be in a folder named Prefabs however it s good to be well organized ;) Firing the projectile 22. Return to the Shooter script we have been working before function Update () { var h : float = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed; var v : float = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed; transform.translate(h,v,0.0); if(input.getbuttonup("fire1")) { This IF statement listens for the key applied to the Input button named Fire1 to be released Left Ctrl OR left mouse button (unless you change it from Input Manager Edit Project Settings Input) - 8 -
Using Instantiate() to spawn/create objects The Instantiate command/function requires 3 pieces of information namely Instantiate(what to make, where to make it, a rotation to give it) 23. Change Shooter script as below: var bullet : Rigidbody; var power : float = 1500; var movespeed : float = 5; function Update () { var h : float = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed; var v : float = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed; transform.translate(h,v,0.0); if(input.getbuttonup("fire1")) { var instance:rigidbody; instance = Instantiate(bullet, transform.position, transform.rotation); Create an instance of whatever object or prefab is assigned to the bullet variable Create this instance at the position and rotation from the transform component of the object that this script is attached to (in our case the MainCamera) Adding a force to the Rigidbody Having now created our object, we need it to be immediately fired forward using the AddForce() command. This command works as follows: Rigidbody.Addforce(Direction and amount of force expressed as a Vector3) - 9 -
Vector3 is a data type, a vector, with 3 components e.g. Vector3 (2,4,1); 24. Shooter script: var bullet : Rigidbody; var power : float = 1500; var movespeed : float = 5; function Update () { var h : float = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed; var v : float = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed; transform.translate(h,v,0.0); if(input.getbuttonup("fire1")){ var instance:rigidbody; instance = Instantiate(bullet, transform.position, transform.rotation); var fwd:vector3; fwd = transform.transformdirection(vector3.forward); We have created a Vector3 type variable and name it fwd. We told it to represent the forward direction of the current transform this script is attached to (Main Camera) δθλαδι να ζχει τθν κατεφκυνςθ προσ τθν οποία κοιτάηει θ κάμερα μασ Το TransformDirection χρθςιμοποιείται για να μετατρζψει ζνα local direction ςε world direction Vector3.forward κα μποροφςαμε να γράψουμε και Vector3(0,0,1) δθλαδι θ κατεφκυνςθ προσ τθν οποία βλζπει θ κάμερα ςτο τοπικό ςφςτθμα - 10 -
ςυντεταγμζνων τθσ (πάντα οι κάμερεσ βλζπουν by default προσ τον Ζ τουσ άξονα) 25. Shooter script: var bullet : Rigidbody; var power : float = 1500; var movespeed : float = 5; function Update () { var h : float = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed; var v : float = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed; transform.translate(h,v,0.0); if(input.getbuttonup("fire1")) { var instance:rigidbody; instance = Instantiate(bullet, transform.position, transform.rotation); var fwd:vector3; fwd = transform.transformdirection(vector3.forward); instance.addforce(fwd*power); We applied the force by first referring to the variable that represents the newly created object, i.e. the variable instance Then using the AddForce command we add a force in the direction of fwd variable multiplied by the power (the variable that we created earlier) in order to do it faster. Save your script!! - 11 -
26. Assign the Projectile prefab to the Bullet public variable of the Shooter Script of Main Camera: Drag the Projectile prefab from the Project panel onto the Bullet variable at Inspector where it currently says None (Rigidbody) PLAY THE GAME!!!! - 12 -