CST8237编程 写作、 辅导Java,Python编程

” CST8237编程 写作、 辅导Java,Python编程CST8237: Games DevelopmentLab 2: Roll a Ball13 January 2017Part IIn this assignment we are going to create a very simple game as an introduction to the basics of Unity3D. Youwill not be using any pre-made assets for this game. This project is covered in the Unity3D documentation in a(video) tutorial: https://unity3d.com/learn/tutorials/projects/roll-a-ball. There is no need to followthe videos from the tutorial (unless you really want to). Here you can find complete and detailed instructionsfor this assignment.Remember, if this is the first time you open Unity, that you must choose the Free License version and login inyour account (you will have to Create one if you havent done so yet).As a general rule for the rest of the lab (and for developing in Unity, in general!), remember to: Play-test continuously to see what happens When you add new objects or change some functionality. Remember to exit play-test mode when you are done testing. Changes made in play-test mode will notbe saved anywhere! Use duplicate and prefabs wherever you can. Organize your elements in the Project View in folders, and nest game objects via Empty Game Objects. Save your scene regularly.The Editor1 The Scene View: Where the game is visually constructed. The Game View: When the Play button is pressed, allows to see the game as itll be built. The Hierarchy View: Complete list of every object in the current scene. The Project Panel: Contains all assets (animations, models, scripts, sound, audio, prefabs) that canbe used in the game. The Inspector Panel: Allows to add, modify and remove components from all objects in the scene. File Build Settings…, allows to build the game for any platform.Unity3D Hotkeys: https://docs.unity3d.com/Manual/UnityHotkeys.html1 Setting up the projectCreate a new project from File New Project. Choose a directory where you want to store your project anda name. You dont need to import any packages and make sure that the defaults are set for 3D in the selector.You can re-arrange the panels in your view as you like, by choosing an option in the top right selector calledLayout. You can also Modify manually your panels. A nice distribution is the one shown in this image:Now save the current (opened by default) scene. Go to File Save Scene, give it a name and a locationwithin the project folder. A recommended place is to put it in a new folder that you create for all scenes, in theAssets folder. You should be able to see your scene in the Project View panel.2 Creating the first game objectsWell start creating the floor of the game. You can create game objects in different ways. This time, go to theHierarchy View and click on Create. Select 3D Object Plane.In general, its always a good practice to create new objects located in the origin of coordinates (0, 0, 0).Unity does not always do this by default (it depends on where your cursor is in the Scene View), but you caneasily set the game Objects position, rotation and scale to its default value: with the plane object selectedin the Hierarchy View, you should see its components in the Inspector View. The first component should bea Transform component, with a gear-like button in the top right corner of the component. Click on it andselect the option reset. This will set the transform of the plane to position=(0, 0, 0), rotation=(0, 0, 0) andscale=(1, 1, 1).2Another good practice is to give all game objects in your scene a meaningful name. In the Hierarchy View,do right click on the plane object and rename it to ground or floor.At this point, we only have one game object in the scene, so it is easy to locate it. However, as soon as thereare may objects, you can easily center the view in the Scene View either by double-clicking in the plane in theHierarchy View, or by selecting it and, with the mouse over the Scene View, press the key F.Now, well scale the game object. You can either do it manually, using the scale tool and dragging the axis,or by modifying the scale values in its transform component. Set the scale to (2, 1, 2).Now, lets add a Sphere to the scene. With no elements selected in the Hierarchy View1, select the option inthe menu GameObject 3D Object Sphere. Name the sphere Player. Reset the sphere, as you did withthe plane, to be sure its located at the center of coordinates.If you center the view on the sphere, you will realise that its buried in the plane. This is because the centerof the sphere is at the origin of coordinates. To put the sphere on the ground, we need to translate it half a unitin the positive Y Coordinate (the sphere radius is, by default, 1 unit). As before, you can change its translationwith the Translate tool, but itll be easier to set it in the transform to (0, 0.5, 0). After this, youll see that thesphere is laying perfectly on the floor of the scene.3 Adding lightsSelect the default light DirectionalLight in the Herarchy View, and delete it. Now, lets add a better light. Clickon Light Directional Light, give it a nice name and reset its position. Well change now its rotation (in itstransform component) to (30, 60, 0) to affect how the lights affects the scene. For directional lights, the positionis not important, as it affects all objects equally.Another component in the light game object is Light. One of the parameters of this component determinesif the light should cast shadows on the objects that it illuminates. Lets add shadows by selecting Soft Shadowsin the Shadow Type selector and Very High Resolution in the Resolution option.Now well add a coloured light. Given that we have already a light in the scene, right click on it and selectDuplicate. Change its name to Fill Light and its rotation to (30, 60, 0). This will be hitting our scenefrom below. In the Light component of the new light, set its intensity to 0.2 to dim the light, and change itscolor to a pale blue (i.e. RGB = (173, 255, 255)). We dont need shadows from this light, so you can turn themoff.Finally, lets organize these Two lights in the Hierarchy View. Create an empty game object and reset it tothe origin. Give it a meaningful name that indicates that it will contain all lights of the game (i.e. Lights).Then, drag and drop both lights into the new game object, in the Hierarchy View; this will add these lights aschildren of the new game object. Note that it is important to have reset the parent before adding the children:otherwise, the children will inherit the transformations (translations, rotations, etc.) of the parent. In otherwords, the transformation of an object that is a child of another are relative to the one of the parent.1Note: with an element selected, you would be adding the new object as a child!34 Moving the playerSelect the sphere (player) on the Hierarchy View. We want this player to collide with other elements on the scene,which requires physics. For this we need to add a new component to the game object, of type rigidbody. Withthe player selected, click on the Add Component button on the Inspector View. Select Physics Rigidbody.In order to capture the players input and move the sphere, we need a script. In the Project View, create anew folder to keep our scripts (name it, for instance, Scripts). There are several ways of creating a new script.In this case, you will select the player in the Hierarchy View, and add a new component of type New Script,giving it the name PlayerController. Make sure that CSharp is selected for the language of the script. Thisoperation creates a new script and also attaches it to the game object selected. The script will appear now asa new component of the player, and you can double click on it to open it (note that, by creating the script likethis, you have to move the script to the Scripts folder).Once the script has been Open in MonoDevelop, youll see that the script has this aspect:1 using UnityEngine ;2 using System . Collections ;34 public class PlayerController : MonoBehaviour {56 // Use this for initialization7 void Start () {89 }1011 // Update is called once per frame12 void Update () {1314 }15 }Delete the contents of the class and write the following:1 using UnityEngine ;2 using System . Collections ;34 public class PlayerController : MonoBehaviour {56 public float speed ;78 void FixedUpdate ()9 {10 float horAxis = Input . GetAxis ( Horizontal ) ;11 float verAxis = Input . GetAxis ( Vertical ) ;12 Vector3 movement = new Vector3 ( horAxis , 0.0 f , verAxis ) ;1314 GetComponent Rigidbody () . AddForce ( movement * speed * Time . deltaTime ) ;15 }16 }The main points of this snippet of code are: A public variable speed. We are coding in the FixedUpdate() function, as the operations involve physics. We capture the input using the GetAxis() function in both axis. This takes the input from the arrow keys. We form a vector with the Direction the ball should move. We are moving the player by Applying a physics force. This force is composed of the direction (movement),multiplied by a speed and by Time.deltaTime. The latter value contains the time since the last updatecall, and assures a smooth movement (try to remove it and see what happens!). You can check the meaning of any method by highlighting it in the code and pressing Crtl. (Cmd) + .Now, save the script and go back to the Unity editor. Potential compilation errors should appear in theConsole window (Window Console), but there should be none. You can test the game by pressing the Playbutton at the center top of the screen, and move the player with the arrow keys.4Actually, nothing is moving… why? The variable speed has a default value of 0. As this is a public variable,it will show up in the script component of the player. Look for it and change its value to 500.5 Moving the CameraNow we are going to place the camera in a better position for the game. Select the camera in the HierarchyView and modify its transform, setting the position to (0, 10, 10) and its rotation to (45, 0, 0). Test your gameby clicking on the Play button at the top.While this is nice, its better to move the camera with the player. For doing this, in the Hierarchy View,drag and drop the camera onto the player, so youll make the camera a child object of the player. As we saidbefore, this will make the cameras transform be relative to the players transform, so the position of the camera(0, 10, 10) will be relative to the players position. Without doing anything else, play test your game.Ups! Of course, the sphere is rotating, and the camera inherits this rotation too! This probably was a badidea, for this game. In a normal 3D person shooter, the target of the camera does not rotate around all itsaxis… but here our ball does! So, we need to change this. Well detach the camera from the player: drag anddrop the camera away from the Player in the Hierarchy View.Well control the movement of the camera with another script. Add a new Script component to the Camera,name it CameraController and place it in the appropriate folder. Open the script for editing and youll seeagain the default code for a C# script. Replace it with the following code:1 using UnityEngine ;2 using System . Collections ;34 public class CameraController : MonoBehaviour {56 public GameObject player ;7 private Vector3 offset ;89 void Start () {10 offset = transform . position ;11 }1213 void LateUpdate () {14 transform . position = Player . transform . position + offset ;15 }16 }Highlights of this code: A public game object, the player, target of the camera. A private offset, from the player to the camera. We use start to initialize the offset. This takes the initial position of the camera (note that we can usethis vector because originally the player is at the origin). We modify the position of the camera (through its transform, directly accessible through the variabletransform), by adding the offset to the position of the player. We are doing this on the LateUpdate() method. LateUpdate() is the appropriate place for follow cameras,procedural animations and gathering last known states.Save the script, go back to the editor and play-test your game. Obviously something is missing, the cameradoesnt move with the ball. You have a hint in the console output, there is an error. The problem is that thepublic game object reference player of the script is not assigned. With the camera selected in the HierarchyView, drag and drop the player from the Hierarchy View onto the player field in the script component of thecamera. Play-test the game again, the camera should move now with the player.6 Prefabs: walls and pick-up objectsLets start creating the walls for the game. Create an empty game object in the hierarchy and call it Walls.Reset it. Create an object of type Cube and make it a child of Walls. Give it a name (itll be the west wall)and modify its transform setting its position to (10, 0, 0) and its scale to (0.5, 2, 20.5).5Now, duplicate the wall to create the east wall, giving it an appropriate name. Change its position to(10, 0, 0). Duplicate the east wall to create the north wall. As we need to rotate this one, change its rotationto (0, 90, 0) and its position to (0, 0, 10). Duplicate the north wall to create the south one, and set its positioncorrectly (which one this Would be?). Play-test to see if the collisions work.Well create the pick-up objects now. Lets create a new cube in the hierarchy (dont forget to reset it)and name it PickUp. Now the pick up is at the same location an the player. To make things easier, letsdeactivate the player by unticking the tick at the top of the Inspector View, while this is selected. This willmake the object disappear, and inactive in the game.Lets change the transform of the new pickup cube so location is set to (0, 0.5, 0), its rotation to (45, 45, 45)and its scale to (0.5, 0.5, 0.5). We are going to animate this cube with a script, by allowing it to rotate aroundits center. With the cube selected, add a new C# script component, called Rotator. Place the script in yourScripts folder and open it for editing. Modify the script so it contains the following code:1 using UnityEngine ;2 using System . Collections ;34 public class Rotator : MonoBehaviour {56 // Update is called once per frame7 void Update () {8 transform . Rotate (new Vector3 (15 ,30 ,45) * Time . deltaTime ) ;9 }10 }Things to note: We are using Update() in this occasion. The movement is not an effect of any physics reaction, so theplace to do the changes is in Update(). We are applying an arbitrary rotation in the three axis, smoothing it with the time between frames.Save your changes, go back to the editor and play-test it.Now, it would be a pain to repeat all this process for each pick-up object that you want in your scene. Evenduplicating objects is not a good idea, as you may want to apply later changes and you would have to modifyall objects one by one. This is a good occasion to use prefabs.Create a new folder in your Project View, called Prefabs. Now simply drag the pick up object from thehierarchy and drop it into the new folder. This simple step creates the prefab. Now you can delete the pickupfrom the hierarchy, As this game object is saved as a prefab with all its components and settings.The prefab asset is like a blueprint of your game object. You can drag and drop the prefab in the hierarchyto create copies of this prefab in the scene, and any change you make in the prefab (by having the prefabselected in the Project View, not selecting a copy in the scene), youll make the change effective in all copies ofthe prefab in the scene.Lets then add the pick-ups. First, create an empty object in the hierarchy to hold all the pick-up objects,and give it a proper name. Did you forget to reset it? Then do it! Now, drag and drop as many pick-ups asyou want from its prefab to the hierarchy. Make all of them children of your pick-up container.Once this is done, distribute all pick-ups around your level. You can do this manually by clicking on theY cone of the gizmo of the upper right of the Scene View. Iteratively, select each pickup object and place itwherever you want in the XZ plane, by using the arrows displayed in the Scene View (note: make sure the viewis in global coordinates):Activate the player back, by ticking the tick near its name on the Inspector View panel. You should see itreappearing in the scene. Play-test your game. Youll see that the player collides with the pickups, and all ofthem are rotating around their axis.67 Collisions vs. Triggers: picking pick-upsIf we dont want to have a physical reaction when colliding with a collider, we must indicate that the colliderof the game object must act as a trigger.Select the pick-up prefab in the Project View and look for the Box Collider component in the InspectorView. You will see that there is a field called IsTrigger, unchecked. Check it and this collider will act as atrigger collider. As you are modifying the prefab, you can check how all pick-ups in the scene have now thisfield ticked.If you play-test the Game now, youll see that the sphere goes through the pick-ups without any physicalreaction. We need to detect this collision in a script, and well do it in PlayerController. Open the script andadd the following function:1 void OnTriggerEnter ( Collider other )2 {3 if( other . gameObject . tag == PickUp )4 {5 other . gameObject . SetActive ( false ) ;6 }7 }What is this doing? OnTriggerEnter() is a function that is called when other game objects collider, configured as a trigger,collides with the collider of the game object that has this script as a component (i.e., in this case, when apick-up collider collides with the players collision). We need to check if we are colliding with a pick-up. For this we check the tag of the game object. If we are colliding with a pick-up, we set the game object to inactive.We still havent set any tag to the pick-up objects, so lets do it now. Again, select the pick-up prefab inthe Project View. You will see then, in the top of the Inspector panel, a selector named Tag, with the valueUntagged. Unity includes several default and useful tags for your game objects, and also allows you to createnew ones.We are going to create a new tag clicking on this selector, and then in the sign +. Then type PickUp asthe name of our new tag in the field Tag 0. This adds the new tag to the list of available tags, but it does notassign the tag to the game object. Therefore, we need to select our prefab again, and assign the tag in the tagoption in the Inspector View. Again, after doing this, you can check that all pick-up game objects have beenassigned this new tag, as we have modified the prefab for these objects.Finally, play-test your game and check that the pick-up objects are now destroyed when the player collideswith them.78 Counting, displaying the count and finishing the gameCounting the number of pick-ups that are collected is quite straightforward. Well do this in the PlayerControllerscript. Perform the following steps: Add a private int count variable to count how many pick-ups are collected. Add the method Start, and initialize this variable to 0. Add 1 to this value every time a pick-up collides with the player.Now, we want to add a text to the screen to display the count of collected pick-ups2.First of all, create a Canvas object by clicking on Create UI Canvas. Then, with the canvas objectselected, do right click on UI Text to create a new text field. Give a name to this game object (i.e.ScoreText) and, in the Inspector View panel, change its default text to something like Score: 0, set the fontcolor to white and the font size to 24.In order to locate this Text on the screen, you must click on the button at the top left corner of the RectTransform component of the text game object:Press Alt and Shift to reveal pivot and position, after clicking this button. Then, select the top left optionof the available choices. You should see the text location on the screen, in the Game View panel.Duplicate the text you have just created and give it another name (i.e. WinText). Change its defaulttext to something like You Win! and its horizontal alignment to center. Repeat the process that positionsthe text on the screen, but this time choose the middle center option (this should place the text in the centerof the screen).Finally, well add code to the PlayerController script to manage how this text is displayed. Modify the codeso it looks like this:1 using UnityEngine ;2 using System . Collections ;3 using UnityEngine . UI ;45 public class PlayerController : MonoBehaviour {67 public float speed ;8 private int count ;9 private int numPickups = 8; // Put here the number of pickups you have .10 public Text scoreText ;11 public Text winText ;1213 void Start ()14 {15 count = 0;16 winText . text = ;17 SetCountText () ;18 }2Note that this is done differently here from the online tutorial. In the videos, the version of Unity used is 4.5, whereas we areusing 5.2. The instructions Here show what youll find in Unity3D 5.2 (GUI elements are one of the main changes after version 4.5).81920 void FixedUpdate () {…} // NO changes2122 void OnTriggerEnter ( Collider other )23 {24 if( other . gameObject . tag == PickUp )25 {26 other . gameObject . SetActive ( false ) ;27 count ++;28 SetCountText () ;29 }30 }3132 private void SetCountText ()33 {34 scoreText . text = Score : + count . ToString () ;35 if( count = numPickups )36 {37 winText . text = You win ! ;38 }39 }40 }Note that we have added a new using statement to be able to use the class Text. Now save the script, goback to the editor, and assign the text to the variables in the script component of the player. Play-test yourgame to check that the score goes up After every pick up and that it displays the You win! message when thegame is over.如有需要,请加QQ:99515681 或WX:codehelp

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导