Unit 2

https://learn.unity.com/tutorial/unit-2-introduction?uv=2021.3&pathwayId=5f7e17e1edbc2a5ec21a20af&missionId=5f71fe63edbc2a00200e9de0&projectId=5cdcc312edbc2a24a41671e6#5d1ba822edbc2a002175788d

Introduction

we will make a top down prototype where you shoot things that come after you, kindof like galiga except in 3d and not at all like galiga XD

Lesson 2.1 - Player Positioning

Summary
Overview:
You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food you would like to feed those animals. You will give the player basic side-to-side movement just like you did in Prototype 1, but then you will use if-then statements to keep the Player in bounds.

Project Outcome:

The player will be able to move left and right on the screen based on the user’s left and right key presses, but will not be able to leave the play area on either side.

Materials

Prototype 2 - Starter Files.zip

Select your Unity version
2021.1 - 2021.3

1.Create a new Project for Prototype 2







2.Add the 3d assets + first few lines of code

    1. If you want, drag a different material from Course Library > Materials onto the Ground object
    2. Drag 1 Human, 3 Animals, and 1 Food object into the Hierarchy
    3. Rename the character “Player”, then reposition the animals and food so you can see them
    4. Adjust the XYZ scale of the food so you can easily see it from above

 

    • Attach the script to the Player and open it
    • In your Assets folder, create a “Scripts” folder, and a “PlayerController” script inside
    • At the top of PlayerController.cs, declare a new
    •  [SerializeField]
    • private float horizontalInput
    • In Update(), set horizontalInput = Input.GetAxis(“Horizontal”), then test to make sure it works in the inspector
using UnityEngine;

public class Movement : MonoBehaviour
{
    [SerializeField]
    private float horizontalInput;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
    }
}

 

 

4.Move the player left-to-right


using UnityEngine;

public class Movement : MonoBehaviour
{
    [SerializeField]
    private float horizontalInput;
    [SerializeField]
    private float speed = 30.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);
    }
}

 

5+6. Keep the player inbounds

New Functionality

The player can move left and right based on the user’s left and right key presses

The player will not be able to leave the play area on either side
New Concepts & Skills
Adjust object scale
If-statements
Greater/Less than operators
Next Lesson, We’ll learn how to create and throw endless amounts of food to feed our animals!


now use chat gpt to teach me thing and fix a bug that can occur when you move fast

float clampedX = Mathf.Clamp(desiredpoz.x, -Xrange, Xrange) become the min/max

using UnityEngine;
//needed a bit of editing coz cgpt3.5 is stoooooooopid
public class Movement : MonoBehaviour
{
    [SerializeField]
    private float speed = 10.0f;
    [SerializeField]
    private float xRange = 10.0f;
    [SerializeField]
    private float horizontalInput;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");

        // Calculate the desired position based on the input
        Vector3 desiredPosition = transform.position + Vector3.right * -horizontalInput * Time.deltaTime * speed;

        // Clamp the desired position within the x range
        float clampedX = Mathf.Clamp(desiredPosition.x, -xRange, xRange);
        desiredPosition = new Vector3(clampedX, desiredPosition.y, desiredPosition.z);
        // Move the object to the clamped position
        transform.position = desiredPosition;
    }
}

3. Instantiation

The first thing we must do is give the projectile some forward movement so it can zip across the scene when it’s launched by the player.

1.Make the projectile fly forwards

using UnityEngine;

public class Pizza_gun : MonoBehaviour
{
    public GameObject Ammo;

    public float Weapon_Bullet_Projectile_Speed = 60.0f;
    // Start is called before the first frame update
    void Start()
    {
        Ammo = GetComponent<GameObject>();
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * Weapon_Bullet_Projectile_Speed);
    }
}

using UnityEngine;

public class Movement : MonoBehaviour
{
    [SerializeField]
    private float speed = 10.0f;
    [SerializeField]
    private float xRange = 10.0f;
    [SerializeField]
    private float horizontalInput;
    [SerializeField] 
    public GameObject ProjectilePrefab;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {


        horizontalInput = Input.GetAxis("Horizontal");
        // Calculate the desired position based on the input
        Vector3 desiredPosition = transform.position + Vector3.right * -horizontalInput * Time.deltaTime * speed;
        // Clamp the desired position within the x range
        float clampedX = Mathf.Clamp(desiredPosition.x, -xRange, xRange);
        desiredPosition = new Vector3(clampedX, desiredPosition.y, desiredPosition.z);
        // Move the object to the clamped position
        transform.position = desiredPosition;
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // shoot pizza
            Instantiate(ProjectilePrefab, transform.position, ProjectilePrefab.transform.rotation);
        }
    }
}

5.Make animals into prefabs

6.Destroy projectiles offscreen

7. Destroy animals offscreen

8.Lesson Recap

New Functionality
  • The player can press the Spacebar to launch a projectile prefab,

Projectile and Animals are removed from the scene if they leave the screen

New Concepts & Skills
  • Create Prefabs
  • Override Prefabs
  • Test for Key presses
  • Instantiate objects
  • Destroy objects
  • Else-if statements

Next Lesson

(

Lesson 2.2 - Food Flight
Skills practiced:
Absolute Beginner Code Comprehension
Interpret simple code
Improve simple code using the features of an IDE
Absolute Beginner Application Scripting
Use common logic structures to control the execution of code.
Write code that utilizes the various Unity APIs
Implement appropriate data types
Write code that integrates into an existing system
Implement a code style that is efficient and easy to read
Prototype new concepts
)

Random Animal Stampeed

1. Create a spawn manager

2. Spawn an animal if S is pressed


3. Spawn random animals from an array

4. Randomize the spawn location

5. Change the perspective of the camera

6. Lesson Recap

New Functionality

New Concepts & Skills

Next Lesson

Collision Decisions + GAME OVER

1. Make a new method to spawn animals

2. Spawn the animals at timed intervals

3. Add collider and trigger components

4. Destroy objects on collision

5. Trigger a “Game Over” message

6. Lesson Recap

New Functionality
  • Animals spawn on a timed interval and walk down the screen
  • When animals get past the player, it triggers a “Game Over” message
  • If a projectile collides with an animal, both objects are removed
New Concepts & Skills
  • Create custom methods/functions
  • InvokeRepeating() to repeat code
  • Colliders and Triggers
  • Override functions
  • Log Debug messages to console

    Pizza_gun
    using UnityEngine;
    
    public class Pizza_gun : MonoBehaviour
    {
        [SerializeField]
        public float Weapon_Bullet_Projectile_Speed = 60.0f;
        [SerializeField] 
        private float topBound = -60;
    
        // Update is called once per frame
        void Update()
        {
            transform.Translate(Vector3.forward * Time.deltaTime * Weapon_Bullet_Projectile_Speed);
    
           if (transform.position.z < topBound)
            {
                Destroy(gameObject);
            }
    
        }
        private void OnTriggerEnter(Collider other)
        {
            Destroy(gameObject);
            Destroy(other.gameObject);
        }
    }
    
    Spawn_manager
  • using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class SpawnManager : MonoBehaviour
    {
        public GameObject[] animalPrefabs = new GameObject[10];
        [SerializeField] 
        private float spawnRangeX = 20.0f;
        [SerializeField] 
        private float spawnPozY = -20.0f;
        [SerializeField]
        private float startDelay = 5f;
        [SerializeField]
       private float spawnInterval = 2f;
        // Start is called before the first frame update
        void Start()
        {
            // invoke repeating, will make the method or void def function with name "nameInQuotes"
            // it will call that function after , delay, then chozen interval
            InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval);
        }
    
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.S))
            {
               SpawnRandomAnimal();
            }
        }
        void SpawnRandomAnimal()
        {
            // Select a random index within the array range
            int random_animal_Index = Random.Range(0, animalPrefabs.Length);
            Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), spawnPozY, -30);
            // Instantiate the selected animalPrefab
            Instantiate(animalPrefabs[random_animal_Index], spawnPos,
                animalPrefabs[random_animal_Index].transform.rotation);
        }
    }
    
  • movement

    using UnityEngine;
    
    public class Movement : MonoBehaviour
    {
        [SerializeField]
        private float speed = 10.0f;
        [SerializeField]
        private float xRange = 10.0f;
        [SerializeField]
        private float horizontalInput;
        [SerializeField] 
        public GameObject ProjectilePrefab;
        // Start is called before the first frame update
        void Start()
        {
    
        }
    
        // Update is called once per frame
        void Update()
        {
    
    
            horizontalInput = Input.GetAxis("Horizontal");
            // Calculate the desired position based on the input
            Vector3 desiredPosition = transform.position + Vector3.right * -horizontalInput * Time.deltaTime * speed;
            // Clamp the desired position within the x range
            float clampedX = Mathf.Clamp(desiredPosition.x, -xRange, xRange);
            desiredPosition = new Vector3(clampedX, desiredPosition.y, desiredPosition.z);
            // Move the object to the clamped position
            transform.position = desiredPosition;
            if (Input.GetKeyDown(KeyCode.Space))
            {
                // shoot pizza
                Instantiate(ProjectilePrefab, transform.position, ProjectilePrefab.transform.rotation);
            }
        }
    }
    

    Animal Movement

    using UnityEngine;
    
    public class AnimalMovement : MonoBehaviour
    {
    
    
        public float Animal_Movement_Speed = 50.0f;
        public float lowerBound = 50;
        // Start is called before the first frame update
        void Start()
        {
    
        }
    
        // Update is called once per frame
        void Update()
        {
            transform.Translate(Vector3.forward * Time.deltaTime * Animal_Movement_Speed);
            if (transform.position.z > lowerBound)
            {
                Destroy(gameObject);
                Debug.Log("In the vernacular of your people... \n Game Over!");
            }
        }
    }