Question

In: Computer Science

View the following C# Script carefully. Assume that the Script is enabled, is attached to an...

View the following C# Script carefully. Assume that the Script is enabled, is attached to an active GameObject, and addresses the appropriate namespaces first.

  1. Discuss when and how often during the game the Update() function will be executed.
  2. Discuss each line (from line 9 to 17, excluding the lines with curly brackets) to detail what the code inside the update function is doing.
    Be sure to outline how the variables are being used during your discussion.

1 public class Player : MonoBehaviour
2 {
3    public GameObject coinPrefab;
4 private GameObject coin;
5    public Transform spawnpoint;
6  
7    void Update()
8    {
9       if (Input.GetMouseButtonDown(0))
10       {
11          Instantiate(coinPrefab, spawnpoint.position, spawnpoint.rotation);
12       }
13       if (Input.GetMouseButtonDown(1))
14       {
15          coin =  GameObject.FindWithTag("Coin");
16          Destroy(coin);
17       }
18    }
19 }

Solutions

Expert Solution

the Update() function is called once for each frame. We use Time.deltaTime to calculate time difference between 2 update calls

I've added comments above each line for explanation

1 public class Player : MonoBehaviour
2 {
3 public GameObject coinPrefab;
4 private GameObject coin;
5 public Transform spawnpoint;
6
7 void Update()
8 {
//if primary button of mouse is pressed (primary button is left click)
9 if (Input.GetMouseButtonDown(0))
10 {
// create a clone of coinPrefab at specific position(spawnpoint.position) and specific angle(spawnpoint.rotation)
11 Instantiate(coinPrefab, spawnpoint.position, spawnpoint.rotation);
12 }
//if secondary mouse button is pressed (right click)
13 if (Input.GetMouseButtonDown(1))
14 {
// returns a game object which is tagged "Coin" and store it in variable coin. If none found, it returns null
15 coin = GameObject.FindWithTag("Coin");
//Destroy the coin object retrieved in previous line
16 Destroy(coin);
17 }
18 }
19 }


Related Solutions

View the following C# Script carefully. Assume that the Script is enabled, is attached to an...
View the following C# Script carefully. Assume that the Script is enabled, is attached to an active GameObject, and addresses the appropriate namespaces first. Discuss when and how often during the game the Update() function will be executed. Discuss each line (from line 9 to 17, excluding the lines with curly brackets) to detail what the code inside the update function is doing. Be sure to outline how the variables are being used during your discussion. 1 public class Player...
I have attached the following script file Q1) List the full name of members, their DETAIL_DAILYLATEFEE...
I have attached the following script file Q1) List the full name of members, their DETAIL_DAILYLATEFEE and the average of DETAIL_DAILYLATEFEE for all members whose DETAIL_DAILYLATEFEE is less than the average of all DETAIL_DAILYLATEFEE. Your query MUST contain a subquery. Your result should be as shown below. Note that each member is only listed once. Required: AVG(DETAIL_DAILYLATEFEE) CONCAT DISTINCT Restricted: GROUP BY Q2) Management is also interested in finding out the age of their movies (i.e. how old the movies...
Write a script in C that will do the following : 1. Create the directory ZHW3....
Write a script in C that will do the following : 1. Create the directory ZHW3. 2. Verify that the directory is created by display all information related to the directory and not just the name of the directory. 3. Assume that the input from the command is used to validate password strength. Here are a few assumptions for the password string. • Length – minimum of 8 characters. • Contain alphabets , numbers , and @ # $ %...
Please read the instructions and  find attached for the first wiki . Instructions for students: Read carefully...
Please read the instructions and  find attached for the first wiki . Instructions for students: Read carefully the attached document and then post your comments bearing in mind the following questions: 1- What are the pros and cons of rent controls? 2- Why economists disagree on the usefulness of rent control? 3- Do you believe rent control can help the poor? Edit Wiki Content rent control Rent regulation can take various forms, including rent control (the placing of a cap on...
Add a sphere into Unity. write ONE C# script in Unity that does the following: Set...
Add a sphere into Unity. write ONE C# script in Unity that does the following: Set the sphere to a color of your choice. Start with growing the sphere’s scale at 0.0005/frame until its scale reaches 3, then shrink the sphere’s scale at the same rate until it reaches 1. Repeat. Sphere rotate around the origin in XY plane at radius of 5. position = transform.localPosition; position.x = radius * Mathf.Sin(Time.fixedTime); position.y = radius * Mathf.Cos(Time.fixedTime);
Introduction to Public Health Read the attached document and view the short video. Determine why it...
Introduction to Public Health Read the attached document and view the short video. Determine why it is related to chapter 3. (Public Health Data and Communications) Explain how health communication influences behavior change (s). Feel free to investigate other resource (citation needed) to expand understanding and explanations.
Calculate the following based on 2018 numbers using the attached financial statements for XYZ Corp. Assume...
Calculate the following based on 2018 numbers using the attached financial statements for XYZ Corp. Assume the only variable cost is the cost of goods sold. Survival revenues (EBITDA breakeven – includes interest). B.NOPAT Breakeven c) Interpret these values and indicate what you would expect to happen to them if a large addition is made to fixed assets. Financial Statements for XYZ Corp. Balance Sheet for Period Ending December 31. Assets 2017 2018 Cash and Marketable Securities 40 15 Accounts...
Lab 11 C++ Download the attached program and run it - as is. It will prompt...
Lab 11 C++ Download the attached program and run it - as is. It will prompt for a Fibonacci number then calculate the result using a recursive algorithm. Enter a number less then 40 unless you want to wait for a very long time. Find a number that takes between 10 and 20 seconds. (10,000 - 20,000 milliseconds). Modify the program to use the static array of values to "remember" previous results. Then when the function is called again with...
3.1. Carefully explain the following observations: (i) Silver dissolves poorly in copper at 100°C. (ii) Copper...
3.1. Carefully explain the following observations: (i) Silver dissolves poorly in copper at 100°C. (ii) Copper dissolves poorly in silver at 100°C. (iii) The amount of copper (in atom per cent) that can dissolve in silver at 100°C is greater than the amount of silver (in atom per cent) that can dissolve in copper at 100°C.
USE C# to write this. 1.To the capsule, add a script called “MoveIt”. This will make...
USE C# to write this. 1.To the capsule, add a script called “MoveIt”. This will make the capsule move repeatedly between (3, 0, 0) and (-3, 0, 0), moving 1 unit per second, moving in the positive direction initially. [Note: The capsule should move toward (3,0,0) from its initial location of (0,1,0) and then move toward (-3,0,0), etc.] 2.To the yellow cube, add a script called “RotateIt”. This will make the cube rotate (30, 60, 90) per second. 3. Lastly,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT