CS案例:C#(C sharp) 32004 Game Programmi
当前位置:以往案例 > >CS案例:C#(C sharp) 32004 Game Programmi
2018-09-25


31263 / 32004 Game Programming
Lab Week 2


Getting Started
1. Download the corresponding week’s zip file from the Lab section of UTSOnline.

2. Unzip the project folder and open it in Unity. If there are any warnings about difference in versions, just continue. If this causes any red errors in the console once the project opens, notify the tutor.

3. Within the Weekly folders there are image and executable files starting with “Status…”. These files give you a preview of what is expected for each point percentage below.

Tasks

Points

Requirements

40%

· Open the project folder in Unity. This is similar to the solution from last week so it should look familiar. Open Week2Scene if it is not already open.

· Create an empty GameObject from the GameObject menu and call it “ParentObj” and set clear its position and rotation by selecting the gear icon of the Transform component and clicking “Reset”.

· Apply the PrintAndHide component to the ParentObj instance and change the tag to “Red”.

50% (P)

· Create a Cube primitive from the GameObject menu.

· Scale it to (1.5,0.75,1), reset the position and rotation, and apply the RedMaterial to it.

· Add the cube as a child of ParentObj by dragging it onto the ParentObj in the Hierarchy Window.

· Associate the cube’s Renderer with the ParentObj’s PrintAndHide.rend variable in the Inspector Window.

· Update the prefab reference of RedPrefab in the Project Window so that the prefab has this parent-child relationship. Do this by dragging ParentObj in the Hierarchy Window to RedPrefab in the ProjectWindow and accepting any warning.

· Do the same for the BluePrefab by changing the cube’s material to BlueMaterial, the tag Blue, and replacing the BluePrefab in the Project Window.

· Delete the ParentObj and its child from the Hierarchy Window.

60% (P)

· Modify the LoadAssets script so that when redObj is initiated it is placed at (2,1,0) and the blueObj is at (-2,-1,0).

· Create a new C# script called InputManager and attach it to the MainCamera.

· In InputManager, make a private member variable that is an array of Transforms called “transArray”.




· When the game starts this script should:

o Declare transAray as an array of size 2.

o Assign the Transform components of redPrefab and bluePrefab to this array (tip: search GameObject.FindWithTag(…))

· Important: InputManager should do its starting actions in Start() and LoadAssets should do its starting actions in Awake(). Consider why this is and take note of it!

70% (C)

· In Update() of InputManager, detect whether the player has pressed the “W” (tip: see Input.GetKeyDown(…))

o When pressed, the redObj should be rotated by 45 degree around the z-axis and

the blueObj by -45 degrees around the z-axis. (tip: Search Transform.Rotate(…))

80% (D)

· Go to “Edit->Project Settings->Input” and edit the Fire1 axes to be bound to the “Q” key.

· In Update() of InputManager, detect whether the player has pressed the Fire key (tip: see Input.GetButtonDown(…))

o When pressed, the redObj and blueObj should swap their vertical positions but keep their horizontal positions. (tip: search Transform.position)

o This flip-flopping should happen every time Fire1 is pressed and should

regardless of what their starting position are.

90% (HD)

· In InputManager, when the Fire1 key is released:

o Get the rend variable from the PrintAndHide component on the redObj. Change the color to be a random value between (51.0,0.0,0.0) and (255.0,0.0,0.0). (tip: see Transform.GetComponent<>(), Material.color, and the class Color – note that new Color(…) takes values between 0.0f and 1.0f for standard colors).

o Repeat for blueObj but for the range (0.0,0.0,51.0) and (0.0,0.0,255.0).

o Print both of these new values to the console in the form “Red: statement.

· In PrintAndHide, comment out all the console printing.

100% (HD)

· In InputManager, when the “E” key is pressed:

o If redObj and blueObj have a PrintAndHide component, then remove it.

o If redObj and blueObj do not have a PrintAndHide component, then add it and ensure that the gameobject it is added to is active.

· In PrintAndHide, when the component is added, it should:

o Assign the rend variable if it is not already assigned.

o Ensure that the renderer referenced by rend is enabled.

· Pressing play, then “E”, then “Q” will cause a NullReferenceException error in the console

o Prevent the error such that pressing Play, “E”, then “Q” will still swap the vertical positions of the cubes but not change the cube colors.

· CLEAN, EFFICIENT, ELEGANT CODE!


Submission

You must show your work to your tutor in this lab OR the start of the next lab in order for it to be graded.


在线提交订单