In today’s update we learn how to move on on our own free will and fire a laser!

Movement

Today we learned to go fast! And by fast I mean go Up, Down, Left, and Right with player environment boxing….. now onto the code for the player class.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
	[SerializeField]
	private float speed = 10f;

	[SerializeField]
	private GameObject laserPrefab;

	[SerializeField]
	private float _fireRate = 0.5f;

	private float _nextFire = 0.0f;

    // Start is called before the first frame update
    void Start()
    {
        // take current position = new position (0,0,0)
		transform.position = new Vector3(0, 0, 0);
    }

    // Update is called once per frame
    void Update()
    {
		calculate_movement();
		fire_laser();
    }

	void calculate_movement() {
		float horizontalInput = Input.GetAxis("Horizontal");
		float verticalInput = Input.GetAxis("Vertical");

		// move me
		transform.Translate( new Vector3(horizontalInput, verticalInput, 0) * speed * Time.deltaTime );

		// vertical box me
		transform.position = new Vector3( transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);

		if ( transform.position.x >= 11.3f) {
			transform.position = new Vector3( -11.3f, transform.position.y, 0 );
		} else if ( transform.position.x <= -11.3f ) {
			transform.position = new Vector3( 11.3f, transform.position.y, 0 );
		}
	}

	void fire_laser() {
		if( Input.GetKey(KeyCode.Space) && Time.time > _nextFire ) {
			_nextFire = Time.time + _fireRate;
			Instantiate(laserPrefab, transform.position + new Vector3(0, 0.8f, 0) , Quaternion.identity );
		}
	}
}

The calculate_movement moves the player and the fire_laser calls the shots. Fun fact transform.Translate is great for new positions. Where transform.position is great for platform boxing(where I’m allow to navigate) and also teleporting like the wrapping physics going from left to right in a infinite loop.

Pew pew

We now have lasers. Essentially make a random object and give it a red mask cover. After that…. wire up some code on the player class so that when someone presses the space bar it creates a laser object, additionally don’t forget a cool down timer or else you’ll spawn 100s of laser in a second.

	[SerializeField]
	private float _fireRate = 0.5f;

	private float _nextFire = 0.0f;

	void fire_laser() {
		if( Input.GetKey(KeyCode.Space) && Time.time > _nextFire ) {
			_nextFire = Time.time + _fireRate;
			Instantiate(laserPrefab, transform.position + new Vector3(0, 0.8f, 0) , Quaternion.identity );
		}
	}

Finally write a script and attach it to the laser so that when it his the end of the screen it should destroy itself instead of flying off into eternity avoiding garbage colleciton.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Laser : MonoBehaviour
{
	//speed variable of 8
	[SerializeField]
	private float speed = 8f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // translate laser up
		transform.Translate( Vector3.up * Time.deltaTime * speed );

		if ( transform.position.y > 8f) {
			Destroy(this.gameObject);
		}
    }
}

Nugget

[SerializeField] above any given property value makes it editable in the Unity Wizard for overrides. Useful if you are working with editors who are afraid of code.

	//speed variable of 8
	[SerializeField]
	private float speed = 8f;