辅导CST8237编程设计、 写作C/C++语言程序

” 辅导CST8237编程设计、 写作C/C++语言程序CST8237: Games DevelopmentLab 2: Roll a Ball (cont.)13 January 2017Part II1 Position and VelocityIn this second part we are going to use some of the concepts learnt in the last lecture. First of all, we are goingto add some debug information to our GUI canvas. Create a new text object (PlayerPosition), and place it inthe bottom left corner of the screen. Text alignment must be Left, font color Black and font size 16.Now, following the same Procedure as in the first part, print in this text the players position (taken fromthe players transform) at every step. Note that you can format the float values of the position vector with theC# function ToString(0.00).Repeat the process indicating the velocity (as a vector, with its coordinates) of the player as well. Note thatfor a new text you also need to modify the position of the Rect Transform component. The easiest way to getthe velocity of an object in Unity is to directly read rigidbody.velocity. However, this assumes the objecthas a rigidbody, which is not necessary. Instead, were going to use vector math for this.The velocity can be calculated as the difference between the current and the last position of the object,divided by the time elapsed (Time.deltaTime). Calculate this velocity and print it on the screen over the textwith the position of the player.2 Code organizationThe code in PlayerController class, if youve been following these instructions, is starting to get quite messy.Certainly, all things related to GUI or the game state (score, or victory conditions) shouldnt be in the playerscontroller. Code organization And modularity is essential for maintenance of large project, and game programmingis not an exception.A good practice is to have a separated object for this. Create a new empty object, give it a nice name (GameController,for instance) and create a C# script for it. Move all the following objects from the PlayerControllerclass to GameController : All text fields. The functions that updateThese text should be moved and made public. Variables for the number of pickups and the count of collected ones. As for the GUI elements, create public functions in GameController that update the game state variables.GameController and PlayerController are two objects that need to communicate with each other. GameControllerneeds to ask the player for its position and velocity, while PlayerController must indicate the GameControllerwhen a pickup has been collected. Well set up this two-way communication using two differentmethods:1 The GameController should have now a reference to the player. This can be exposed as a public variablein PlayerController and set from the editor. The PlayerController will hold a (private) reference to the GameController, which is initialized in theStart() method. Set the tag GameController to the GameController object in the editor, and use thefunctions ameObject.FindGameObjectWithTag and GetComponent() to set up this reference in the codeof PlayerController.Also, for some features Needed further on, we are going to give the game controller access to the pickupobjects. Create an array of GameObject in the GameController class, and assign these objects in the editor.3 Back to the MathProbably you have noticed, but velocity As a vector is not very human readable. Modify the code so the textalso reflects the speed as a scalar. Note That you have to get this value from the players velocity, instead ofprinting the value of the variable speed. Why are these different? What are the units of this speed?The next task is to add another text label that indicates the distance to the closest pickup. Place this overthe player velocity text, and add the necessary code to do so in the GameController script. Remember to takeinto account those that have been already collected. In order to highlight which is the closest pickup, changeits material colour to blue with the following line:1 pickup [ i ]. GetComponent Renderer () . material . color = Color . blue ;Remember that you must also set the other colours back to white:1 pickup [ i ]. GetComponent Renderer () . material . color = Color . white ;Lets add now some graphical Debug indicators. Unity provides a class (LineRenderer) that allows to…render lines in the game. Include the following lines in the variable section and the Start() function of theGameController class:1 public class GameController : MonoBehaviour {2 // …3 private LineRenderer lineRenderer ;4 void Start () {5 // …6 lineRenderer = gameObject . AddComponent LineRenderer () ;7 }8 // …9 }To draw a line with a LineRenderer, you must call (at least) the following three functions:1 // 0 for the start point , position vector startPosition 2 lineRenderer . SetPosition (0 , startPosition ) ;3 // 1 for the end point , position vector endPosition 4 lineRenderer . SetPosition (1 , endPosition ) ;5 // Width of 0.1 f both at origin and end of the line6 lineRenderer . SetWidth (0.1 f , 0.1 f ) ;Render one of these lines from the player to the closest pickup identified previously.4 Debug ModesThe next step is to define Different execution modes. The idea is to have the following status of the game,regarding the debug information: Normal: No debug information is shown. Distance: Player position and velocity are shown, including the distance to the closest pickup and theline from the player to this object. All pick-ups are white, with the exception of the closest one, which isrendered in blue (this is the state the game should be at the moment).2 Vision: Described next.In order to switch from one state to the next, capture the input of the key Space, and switch from one tothe next (circularly). Suggestion: use a C# enum type to code this.For the Vision mode, the following features must be implemented: The line renderer must draw a line that shows the current velocity of the player. The starting point shouldbe the position of the player, while the ending point is this position plus its velocity. All pick-ups must be white, with the exception of that one that the player is approaching more directly,which should be rendered Green. This is, using the velocity of the player, determine which pick-up is morelikely to be collected next (even if following the current direction wouldnt actually collide with anyone).Think which vector operation you need to determine this. The green pick-up must be oriented towards the player, instead of performing the perpetual orientationthat they are usually in. Use the function LookAt for this feature. All other white pick-ups (even if theyveturned green in the past) Must keep rotating as before.5 Building the gameWhen the game is finished, you can build it going to File Build Run. Select the platform you want to buildthe game for and the targets attributes on the right. Then, click on Add Current to add the current scene tothe build and press Build. This should prompt you with an output folder to save the standalone built, that youcan run from outside Unity. Select a destination folder and click on Save (this could take some time for largerprojects, but just a few seconds for this one).For this assignment, Generate a build for the Web Browser and test it opening the html file generated byUnity. You should be able to play the game in the browser, and it should look and behave exactly as in theUnity editor when you play-test it.如有需要,请加QQ:99515681 或WX:codehelp

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