Unity Tutorial 07

 


Image from the tutorial

For this weeks tutorial I had to learn how to make the camera follow around my player. My player was a sphere on an island and I had to have the sphere rolling around the island without falling off and I had to have the sphere move in the direction of the camera.  First off I had to make a new GameObject called the focal point and put it in the centre off the scene. I then had to make the camera a child object of the focal point and reset the focal point position to (0,0,0) so that it would change whenever the camera rotated. I then had to make a new script called RotateCamera and attach it to the focal point.  I then had to go into my RotateCamera script and add a new public float called RotationSpeed; I also had to go into my Update method and a new float called horizontalInput = Input.GetAxis("Horizontal"); I also had to add transform.Rotate(Vector3.up, horizontalInput * rotationSpeed * Time.deltaTime); and then set the rotation speed to 50. I then had to make the player move forward. To do this I had to create a PlayerController script as usual and add it to the player. I then had to create a public float speed variable and set it to 5.0f; and create a new private RigidBody called playerRB; I then had to add playerRb =  GetComponent<Rigidbody>(); to my Start method. I then had to go into my Update method and add in float forwardInput = Input.GetAxis("Vertical"); and playerRB.AddForce(Vector3.forward * forwardInput * speed); I then had to get the camera to move in the direction of the focal point.  To do this I had to go into my PlayerController script and add a new private GameObject called focalPoint; I then had to go into my Start method and add in focalPoint = GameObject.Find("Focal Point"); I then had to go into my Update method and add in playerRb.AddForce(focalPoint.transform.forward * forwardInput * speed); After this I had to create an enemy for the player. The enemy sphere had to be able to knock the player off the edge if it got too close. To do this I had to create a new sphere and name it enemy. I then had to give the enemy a RigidBody. I then had to create a new physics material folder and call it bouncy and add it to the player. I then had to set the bounciness to 1 and change the bounce combine to multiply and apply it to my player and enemy. I then had to create a new enemy script and attach it to the enemy. I then had to go into my Enemy script and add a public float speed and set it to 3.0f; I then had to add in a new private RigidBody called enemyRb; and a private GameObject called player; I then had to add enemyRb = GetComponent<Rigidbody>(); and player = GameObject.Find("Player"); to my Start method. Then I had to add enemyRb.AddForce((player.transform.position - transform.position).normalized * speed); I then had to add Vector3 lookDirection = (player.transform.position - transform.position).normalized; to my Update method and remove that line of code from my enemyRb.AddForce and replace that with enemyRb.AddForce(lookDirection * speed); I then had to make a SpawnManager for the enemy. To do this I had to add Enemy to a new prefabs folder and delete it from the scene. I then had to create a new Spawn Manager object and SpawnManager script and add the script to the object. I then had to go into my SpawnManager script and add a public GameObject called enemyPrefab; I then had to add Instantiate(enemyPrefab, new Vector3(0, 0, 6), enemyPrefab.transform.rotation); to my Start method. I then had to spawn the enemy in a random position each time instead of in the same spot. To do this I had to add a new private float called spawnRange and set it to 9.0f; I then had to add float spawnPosX = Random.Range( - spawnRange, spawnRange); and float spawnPosZ = Random.Range( -spawnRange, spawnRange); and Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ); to my Start method. I learned how to make a camera follow the player in all directions it was moving. I also learned how to randomly spawn enemies although I had done this before.

Comments

Popular posts from this blog

Introduction Post

My Favourite Game: Grand Theft Auto Franchise

Game Alpha