So let me preface by saying I am brand new to Unity and I have no coding background. Currently I going through the Junior Programmer pathway on Unity Learn and for a personal project I attempting to remake the Fly Swatter game from Mario Paint. I have managed to script what I can to mimic the movement characteristics of some of the bug enemies. I have provided a clip of the yellow bugs from the original game and a clip from what I have come up with which are the red squares in the clip. Personally just wanted get some professional opinions on how I could do this in alternative ways or more efficiently. I have attached my scripts that I have made. Thanks in advance sorry if I have violated any rules I rarely post on any forums.
peacefulinnocentboa
jaggedslighteel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bugType2 : MonoBehaviour
{
private Vector3 targetPosition;
public float travelToThisPosition = 20;
public float speed = 15;
public int frameCounter = 0;
public int timeToLive = 1200;
private int positionNumber;
// Start is called before the first frame update
void Start()
{
// CheckStartingPosition is called once at the beginning of objects life to verify
// starting position and set the correct value for positionNumber variable
CheckStartingPosition();
}
// Update is called once per frame
void Update()
{
// Here we are correlating the value of positionNumber to determine where starting position is
// based on that a function is called every frame to move in the correct direction
if (positionNumber == 1)
{
Debug.Log("This should move right");
MoveRight();
}
else if (positionNumber == 2)
{
Debug.Log("This should move down");
MoveDown();
}
else if (positionNumber == 3)
{
Debug.Log("This should move left");
MoveLeft();
}
else if (positionNumber == 4)
{
Debug.Log("This should move up");
MoveUp();
}
}
// At the end of a frame the UpdateFrameCounter function is called to update frame counter
private void LateUpdate()
{
UpdateFrameCounter();
}
// This function adds to the frame counter until it reaches a specified amount then destroys object
void UpdateFrameCounter()
{
if (frameCounter < timeToLive)
{
frameCounter++;
}
else
{
Destroy(gameObject);
}
}
// This function checks the position on the specified axis to determine where the starting position is
void CheckStartingPosition()
{
if (transform.position.x <= -26)
{
Debug.Log("Move right");
MoveRight();
positionNumber = 1;
}
else if (transform.position.z >= 26)
{
Debug.Log("Move down");
MoveDown();
positionNumber = 2;
}
else if (transform.position.x >= 26)
{
Debug.Log("Move left");
MoveLeft();
positionNumber = 3;
}
else if (transform.position.z <= -26)
{
Debug.Log("Move up");
MoveUp();
positionNumber = 4;
}
}
// This function moves object right
void MoveRight()
{
targetPosition = new Vector3(-travelToThisPosition, transform.position.y, transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
// This function moves object down
void MoveDown()
{
targetPosition = new Vector3(transform.position.x, transform.position.y, travelToThisPosition);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
// This function moves object left
void MoveLeft()
{
targetPosition = new Vector3(travelToThisPosition, transform.position.y, transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
// This function moves object up
void MoveUp()
{
targetPosition = new Vector3(transform.position.x, transform.position.y, -travelToThisPosition);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
private float spawnRange = 20.0f;
public GameObject bugTypeSelection1;
public GameObject bugTypeSelection2;
public Vector3[] SpawnPos;
private int spawnTotal;
public int frameCounter = 0;
// Update is called once per frame
void Update()
{
int spawnIndex = Random.Range(0, 4);
spawnTotal = 4;
SpawnPos = new Vector3[spawnTotal];
SpawnPos[0] = new Vector3(-26, 0, Random.Range(-spawnRange, spawnRange));
SpawnPos[1] = new Vector3(Random.Range(-spawnRange, spawnRange), 0, 26);
SpawnPos[2] = new Vector3(26, 0, Random.Range(-spawnRange, spawnRange));
SpawnPos[3] = new Vector3(Random.Range(-spawnRange, spawnRange), 0, -26);
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(bugTypeSelection1, SpawnPos[spawnIndex], bugTypeSelection1.transform.rotation);
}
}
private void LateUpdate()
{
if (frameCounter < 1000)
{
frameCounter++;
}
else
{
Instantiate(bugTypeSelection2, new Vector3(0, 0, 0), bugTypeSelection2.transform.rotation);
frameCounter = frameCounter - 1000;
}
}
}