This lesson continues from Unity Basics – Part 2.
Position the Elements in the Scene
Move the Sphere in front of the Player.
Make it smaller, think of it as an obstacle for our Player.
Make the Player Smaller so we have more room to drive.
Now we can Run into stuff!
Change the Color the Sphere
Go to the Materials folder.
Right Click > Create a New Material , name it Sphere1 and Pick a Color (green).
Now Drag this color to the Sphere.
Make a Projectile
Lets make a projectile
Create a 3D capsule and make it squished like a frisbee
Now in Materials folder make a new material and call it ‘projectile’
Now click on Albedo and pick a color
Now drag this Material to the Projectile in the Hierarchy
Add a New Script to the Projectile
Create a new script in the script folder and call it ‘MoveForward’
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveForward : MonoBehaviour
{
public float speed = 40.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward* Time.deltaTime * speed);
}
}
Press Save.
Now go back to Unity.
You might want to change the speed. See where it is going.
If it is going in the wrong direction adjust its Rotation.
Adjust the Speed as you wish.
Make a Prefab Folder in the Assets
Now Drag the Projectile in to this folder
Test it –
Now you can press play and drag from this folder to the hierarchy and it will keep making them
Now Adjust the PlayerController Script so that it can throw the Projectile. You can Paste this new script over the original PlayerController Script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 5.0f;
public float turnSpeed;
//projectlie control
public GameObject projectilePrefab;
public GameObject player;
private float horizontalInput;
private float forwardInput;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
//Move the vehicle forward
transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
//Rotates the car based on horizontal input
transform.Rotate(Vector3.up, turnSpeed * horizontalInput * Time.deltaTime);
//Launch a projectile from the player
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(projectilePrefab, transform.position + new Vector3(0, 0, 0.5f), player.transform.rotation);
}
}
}
Add A Destroy Script
Now Add the Destroy Script so that the Projectiles disappear when they go past the road.
In Scripts create a new C# file and call it DestroyOutOfBounds.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOutOfBounds : MonoBehaviour
{
public float topBound = 30;
public float lowerBound = -20;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (transform.position.z > topBound)
{
Destroy(gameObject);
} else if (transform.position.z < lowerBound)
{
Destroy(gameObject);
}
}
}
Now you should be able to shoot projectiles !
Experiment with adding the MoveForward script to the Sphere’s to make moving obstacles!