Unity - Basic1

    科技2022-07-21  110

    playerController

    Move the vehicle forward

    void Update() { //Move the vehicle forward transform.Translate(0,0,1); } void Update() { //Move the vehicle forward transform.Translate(Vector3.forward); }

    Edit -> Project Settings -> Input

    Map user input with variable

    public float speed = 5.0f; public float turnSpeed; public float horizontalInput; public float forwardInput; void Update() { horizontalInput = Input.GetAxis("Horizontal"); forwardInput = Input.GetAsix("Vertical"); //Move the vehicle forward transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput); //Turn the vehicle left/right transform.Translate(Vector3.right * Time.deltaTime * turnSpeed * horizontalInput); //Let the vehicle rotate instead of slide transform.Rotate(Vector3.up, turnSpeed * horizontalInput * Time.deltaTime); }

    Keep the vehicle in bounds

    public float xRange = 10; void Update() { if (transform.position.x < -xRange) { transform.position = new Vector3(-xRange, transform.position.y, transform.position.z); } if (transform.position.x > xRange) { transform.position = new Vector3(xRange, transform.position.y, transform.position.z); } }

    cameraController

    Let the camera follow the object

    public GameObject player; private Vector3 offset = new Vector3(0, 5, -7); void Update() { transform.position = player.transform.position + offset; }

    GetKey/GetKeyDown/GetKeyUp & Instantiate

    Instantiate (create an object) the projectilePrefab

    public GameObject projectilePrefab; void Update() { if (Input.GetKeyDown(KeyCode.Space)) { //launch a profectile from the player Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation); } }

    Destroy

    Destroy Out of Bounds

    private float topBound = 30; void Update(){ if(transform.position.z > topBound) { Destroy(gameObject); }}

    SpawnManager -> Animal Prefabs (an array)

    Instantiate a random animal in the Animal Prefabs array when pressing “S” at random position

    public GameObject[] animalPrefabs; private float spawnRangeX = 20; private float spawnPosZ = 20; void Update(){ if(Input.GetKeyDown(KeyCode.S)){ Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ); int animalIndex = Random.Range(0, animalPrefabs.Length); Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation); } }

    Method & InvokeRepeating

    Spawn the animal on the timer

    private float startDelay = 2; private float spawnInterval = 1.5f void Start() { //Repeatly call the SpawnRandomAnimal method, starting at 2 seconds, and call it again every 1.5 seconds InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval); } void Update(){ } void SpawnRandomAnimal(){ Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ); int animalIndex = Random.Range(0, animalPrefabs.Length); Instantiate(animalPrefabs[animalIndex], spawnPos, animalPrefabs[animalIndex].transform.rotation); }

    Destroy objects on collision

    private void OnTriggerEnter(Collider other) { Destroy(gameObject); Destroy(other.gameObject); }

    Debug.Log

    If an animal goes past the lower bound, send a message

    void Update() { if (transform.position.z > topBound) { Destroy(gameObject); } else if (transform.position.z < lowerBound) { Debug.Log("Game Over!"); Destroy(gameObject); } }
    Processed: 0.009, SQL: 8