4.1.3 - Toss objects randomly into the air
What is AddForce.Torque
apply a rotational force to a Rigidbody component, causing it to rotate around its center of mass.
4.1.3 - Toss objects randomly into the air
What is the syntax for AddForce.Torque?
AddForce.Torque(Vector3 torque)
4.1.3 - Toss objects randomly into the air
We need to toss four prefabs into the air with a random force, torque, and position. We also want an immediate force, so use the proper ForceMode.
In your “Target” script, write the statements.
private Rigidbody targetRb;
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);
transform.position = new Vector3(Random.Range(-4,4), -6);
}
4.1.3 - Toss objects randomly into the air
We need to toss four prefabs into the air with a random force, torque, and position. Describe what you will need to do in the Target script to achieve this.
4.1.4 - Replace messy code with new methods.
We’re going to store our random force, torque and position in brand new clearly named custom methods.
1. Write new variables min/max speed, spawn positions, min/max torque. HINT: Simplify.
2. Update the code
3. Write new methods for RandomForce, RandomTorque, RandomSpawnPos.
private Rigidbody targetRb;
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);
transform.position = new Vector3(Random.Range(-4,4), -6);
}
Tip: Don’t forget that some methods must return a value
private Rigidbody targetRb;
private float minSpeed = 12;
private float maxSpeed = 16;
private float maxTorque = 10;
private float spawnPosY = -6;
private float spawnPosX = 4;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(RandomForce(), ForceMode.Impulse);
targetRb.AddTorque(RandomTorque(), RandomTorque(), RandomTorque(), ForceMode.Impulse);
transform.position = RandomSpawnPos();
}
**Vector3 RandomForce()
{
return Vector3.up * Random.Range(minSpeed, maxSpeed);
}
float RandomTorque()
{
return Random.Range(-maxTorque, maxTorque);
}
Vector3 RandomSpawnPos()
{
return new Vector3(Random.Range(-spawnPosX, spawnPosX), spawnPosY);
} **4.1.5 - Create an Object list in Game Manager
What is List<> ?
used to store a dynamic array of objects of a specific type T
4.1.5 - Create an Object list in Game Manager
What is the difference between a List and an Array?
With List we can pass in the type of thing that we want, with arrays we have to tell the array what the thing is before we make the array itself.
4.1.5 - Create an Object list in Game Manager
What is the Syntax for List?
List<> stringname
4.1.5 - Create an Object list in Game Manager
Write a List to use for your Targets
public List< GameObject > targets;
4.1.4 - Replace messy code with new methods.
We’re going to store our random force in a brand new clearly named custom methods.
1. Write new variables for min/max speed
2. Update the code
3. Write new method for RandomForce
private Rigidbody targetRb;
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);
transform.position = new Vector3(Random.Range(-4,4), -6);
}
private Rigidbody targetRb;
private float minSpeed = 12;
private float maxSpeed = 16;
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(RandomForce(), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);
transform.position = new Vector3(Random.Range(-4,4), -6);
}
Vector3 RandomForce()
{
return Vector3.up * Random.Range(minSpeed, maxSpeed);
}
4.1.4 - Replace messy code with new methods.
We’re going to store our random torque in a brand new custom method.
1. Write new variables for min/max torque
2. Update the code
3. Write new method for RandomTorque.
private Rigidbody targetRb;
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);
transform.position = new Vector3(Random.Range(-4,4), -6);
}
private Rigidbody targetRb;
private float maxTorque = 10;
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(RandomTorque(), RandomTorque(), RandomTorque(), ForceMode.Impulse);
transform.position = new Vector3(Random.Range(-4,4), -6);
}
float RandomTorque()
{
return Random.Range(-maxTorque, maxTorque);
}
4.1.4 - Replace messy code with new methods.
We’re going to store our random position in a brand new custom method.
1. Write new variables for spawn positions
2. Update the code
3. Write new method for RandomSpawnPos.
private Rigidbody targetRb;
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);
transform.position = new Vector3(Random.Range(-4,4), -6);
}
private Rigidbody targetRb;
private float spawnPosX = 4;
private float spawnPosY = -6;
void Start()
{
targetRb = GetComponent< Rigidbody >();
targetRb.AddForce(Vector3.Up * Random.Range(12, 16), ForceMode.Impulse);
targetRb.AddTorque(Random.Range(-10,10), Random.Range(-10,10), Random.Range(-10,10), ForceMode.Impulse);
transform.position = RandomSpawnPos();
}
** Vector3 RandomSpawnPos()
{
return new Vector3(Random.Range(-spawnPosX, spawnPosX), spawnPosY);
}**
4.1.6 - Create a coroutine to spawn objects
Now that we have a list of object prefabs, we should instantiate them in the game using coroutines and a new type of loop.
In your GameManager script, create a new Coroutine called SpawnTarget
2. Crete and include spawnRate in for seconds
3. Create an Index variable with a random range using your targets List
4. Instantiate the targets index
5. Write the Start co-routine statement
private float spawnRate = 1.0f;
void Start()
{
**StartCoroutine( SpawnTarget () ); **
}IIEnumerator SpawnTarget()
{
while(true)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targets.Count);
Instantiate(targets[index]);
}
}
4.1.6 - Create a coroutine to spawn objects
When using List, we cannot use Length like we do an array. What do we use instead of Length?
Count
4.1.6 - Create a coroutine to spawn objects
In our SpawnTarget method, write the randomized index statement to fill in the code:
IEnumerator SpawnTarget()
{
while(true)
{
yield return new WaitForSeconds(spawnRate);
_________________________________________________
Instantiate(targets[ ]);
}
}
int index = Random.Range(0, targets.Count);
4.1.6 - Create a coroutine to spawn objects
Now that we have most of our SpawnTarget code block written, Instantiate the targets List.
IEnumerator SpawnTarget()
{
while(true)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targets.Count);
_________________________________________________
}
}
Instantiate(targets[index]);
Now that our targets are spawning and getting tossed into the air, we need a way for the player to destroy them with a click. We also need to destroy any targets that fall below the game view and trigger the game sensor.
In your Target script, write methods that destroy the game object when the mouse is clicked, and when the gameobject meets the collider of the object sitting below the camera view.
private void OnMouseDown()
{
Destroy(gameObject);
}
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
}
5.2.1 - Add Score text and position it on screen
We are going to begin adding text in our game. What is the path to TextMeshPro?
Hierarchy > Create button > UI > TextMeshPro
5.2.1 - Add Score text and position it on screen
What is the name of the object all of our text and UI elements appear on?
Canvas
4.2.3 - Initialize score text and variable
what is the name of the library we need to use the different methods and statements with TextMeshPro?
TMPro;
4.2.3 - Initialize score text and variable
We are going to write a variable to keep track of our score in the UI. What script should it go in?
1. Target
2. GameManager
GameManager
4.2.3 - Initialize score text and variable
Write the variable for which we cann the text we made, “scoreText”
public TextMeshProUGUI scoreText;
4.2.3 - Initialize score text and variable
What is TextMeshProUGUI?
4.2.3 - Initialize score text and variable
With our TextMeshUGUI scoreText called in for our UI, what other variable do we need in order to keeep track of a score?
private int score;