Coding:
One of the best part of gaming making is coding, with which results are easy to achieve, for me the basics on coding got quite interesting as I carry to revise them thoroughly.
Some of the basics code that I felt easy were, buttons for scene management, providing options for character like health, attack, destroy, time frame etc. But Some were quite challenging for me to set up by myself which were written by game tutor, Joe.
Here are some of the sample of coding that we did which can be handy for future use and learning.
*Scene management
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainmenuScriptExit : MonoBehaviour
{
public void MainMenuBtn ()
{
SceneManager.LoadScene(0);
}
}
*Destroy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destroy : MonoBehaviour
{
public void DestroyObject()
{
gameObject.transform.position = new Vector3(0, 0, 1000);
if(gameObject.name == "cpuKT")
{
GameObject.Find("GameController").GetComponent<GameController>().gameWin = true;
}
//Destroy(gameObject);
}
}
*Enemy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemy : MonoBehaviour
{
[SerializeField] public int health;
private void Update()
{
if(health <= 0)
{
gameObject.transform.parent.GetComponent<Destroy>().DestroyObject();
}
}
}
*Enemy Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
[SerializeField] private GameObject towerPlaceholder1;
[SerializeField] private GameObject[] EnemyTowers;
private bool tower1empty = true;
public float shootWaitTime1 = 1.5f;
public float shootTimer1;
public float shootWaitTime2 = 1.5f;
public float shootTimer2;
private void Start()
{
shootWaitTime1 = Random.Range(5, 15);
}
private void Update()
{
if(Time.time > shootWaitTime1)
{
int ran = Random.Range(5, 15);
shootWaitTime1 = Time.time + ran;
int ran2 = Random.Range(0, EnemyTowers.Length);
if (tower1empty)
{
tower1empty = false;
GameObject go = Instantiate(EnemyTowers[ran2], towerPlaceholder1.transform.position, Quaternion.identity);
go.transform.SetParent(towerPlaceholder1.transform);
}
}
}
}
*Creating Minions( they are the attacking units with different properties like health, speed, attack, range etc.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class CreateMinion : MonoBehaviour
{
private GameController GC;
public Minion minion;
private Transform goal;
private NavMeshAgent agent;
void Start()
{
GC = GameObject.Find("GameController").GetComponent<GameController>();
minion = GC.activeMinion;
//goal = GameObject.Find("cpuT").transform;
agent = GetComponent<NavMeshAgent>();
agent.speed = minion.speed;
GameObject GO = Instantiate(minion.prefab, gameObject.transform.position, Quaternion.identity);
GO.transform.parent = gameObject.transform;
}
private void Update()
{
/* if (agent.isOnNavMesh == true)
{
agent.destination = goal.position;
}*/
}
}
*Fire Tower Script
a fire tower shoots enemy that are projected by player, it does track and shoot ememies very well. this includes particle effects that follow enemy and reduces its health.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireTower : MonoBehaviour
{
public List<Transform> unitsInRange = new List<Transform>();
public float distanceTo = 99999;
public float damage;
[SerializeField] public Transform target;
private float turnSpeed = 1;
[SerializeField] public ParticleSystem fire;
private void Start()
{
fire.Stop();
}
private void Update()
{
if (target == null)
{
fire.Stop();
FindTarget();
}else if (target != null)
{
fire.Play();
Vector3 targetDir = target.position - transform.position;
float step = turnSpeed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
//Debug.DrawRay(transform.position, newDir, Color.green);
transform.rotation = Quaternion.LookRotation(newDir);
target.GetComponent<MinionController>().curHealth -= damage * Time.deltaTime;
}
}
void UpdateUnits()
{
foreach (Transform t in unitsInRange)
{
t.GetComponent<MinionController>().FindGoal();
}
}
public void FindTarget()
{
foreach (Transform t in unitsInRange)
{
float distance = Vector3.Distance(gameObject.transform.position, t.transform.position);
if (distance < distanceTo)
{
target = t.transform;
}
}
}
}
* Canon shoot( I use this to shoot users units,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cannon : MonoBehaviour
{
public List<Transform> unitsInRange = new List<Transform>();
public float distanceTo = 99999;
[SerializeField] public Transform target;
private float turnSpeed = 1;
//Shooting variables
public float shootWaitTime = 1.5f;
public float shootTimer;
[SerializeField] private GameObject cannonBall;
[SerializeField] private Transform spawnPoint;
private void Update()
{
if(target == null)
{
FindTarget();
}
if (target != null)
{
Vector3 targetDir = target.position - transform.position;
float step = turnSpeed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
Debug.DrawRay(transform.position, newDir, Color.green);
transform.rotation = Quaternion.LookRotation(newDir);
if(Time.time > shootTimer)
{
shootTimer = Time.time + shootWaitTime;
Shoot();
}
}
}
public void FindTarget()
{
foreach (Transform t in unitsInRange)
{
float distance = Vector3.Distance(gameObject.transform.position, t.transform.position);
if (distance < distanceTo)
{
target = t.transform;
}
}
}
void Shoot()
{
GameObject go = Instantiate(cannonBall, spawnPoint.position, Quaternion.identity);
go.GetComponent<cannonBall>().target = target;
}
/* private void OnTriggerEnter(Collider other)
{
if(other.tag == "unit")
{
unitsInRange.Add(other.transform);
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "unit")
{
unitsInRange.Remove(other.transform);
}
}*/
}
*Cannon Ball
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cannonBall : MonoBehaviour
{
float t;
public Transform target;
float speed = 150f;
private Transform startPos;
private void Start()
{
startPos = gameObject.transform;
;
}
void Update()
{
if (target != null)
{
//t += Time.deltaTime / speed;
//transform.position = Vector3.Lerp(startPos.position, target.position, t);
transform.position = Vector3.MoveTowards(startPos.position, target.position, speed * Time.deltaTime);
}
}
}
these elisted scripts are working scripts that have been corrected overtime. Me recording Joe's class activities did help a lot to correct at the end.
My next goal is to learn to code these by myself which would be a good skill to have.
I am very much satisfied with the results. so far. and it works perfectly with the game.
This image was taken after the all the enemies got killed by the AI's defense tower making them unable to rich Crown towers. This was because I changed some destroy value for for Ai defenses mainly, Inferno tower and Transformer. Both of them are functioning well.
No comments:
Post a Comment