🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Unity Unit Testing in Play Mode

Started by
0 comments, last by Shayalp1 3 years, 8 months ago

Hi guys, I've created a simple game similar to gold miner, but this is my first attempt at Unit Testing. I'm trying use the in built Unity Test Runner but I'm not sure where to begin with. I'm trying to test for simple task in which the hook should drop upon click event listener/ trigger.

These are the code for the hook

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

public class HookMovement : MonoBehaviour{

//Rotation of the hook on the z-axis
public float min_Z = -55f, max_Z = 50f;
public float rotate_Speed = 5f;

private float rotate_Angle;
private bool rotate_Right;
private bool canRotate;

public float move_Speed = 3f;

// Pulling speed of the hook
private float initial_Move_Speed;

// How far can the hook extend
public float min_Y = -4f;
private float initial_Y;

private bool moveDown;

//For Line Renderer
private RopeRenderer ropeRenderer;

void Awake() {
ropeRenderer = GetComponent<RopeRenderer>();
}

void Start() {

initial_Y = transform.position.y;
initial_Move_Speed = move_Speed;

canRotate = true;
}

void Update(){
Rotate();
GetInput();
MoveRope();
}

void Rotate(){
if(!canRotate)
return;

if(rotate_Right){
rotate_Angle += rotate_Speed * Time.deltaTime;
} else{
rotate_Angle -= rotate_Speed * Time.deltaTime;
}

//Rotate the hook in an angle on the z-axis
transform.rotation = Quaternion.AngleAxis(rotate_Angle, Vector3.forward);

if(rotate_Angle >= max_Z){
rotate_Right = false;
} else if(rotate_Angle <= min_Z){
rotate_Right = true;
}
}// Rotate hook

void GetInput(){

if(Input.GetMouseButtonDown(0)) {

if(canRotate) {
canRotate = false;
moveDown = true;
}
}
}// Stop rotating the hook and move the rope

void MoveRope() {

if(canRotate)
return;

if(!canRotate) {
SoundManager.instance.RopeWhip(true);
Vector3 temp = transform.position;

if (moveDown) {
temp -= transform.up * Time.deltaTime * move_Speed;

} else {
temp += transform.up * Time.deltaTime * move_Speed;
}

transform.position = temp;

if(temp.y <= min_Y) {
moveDown = false;
}//Move the rope up when it reaches minimum y-axis

if(temp.y >= initial_Y) {
canRotate = true;

// deactivate line renderer
ropeRenderer.RenderLine(temp, false);

// reset move speed
move_Speed = initial_Move_Speed;

SoundManager.instance.RopeWhip(false);
}

ropeRenderer.RenderLine(transform.position, true);
}

}// Move Rope


public void HookAttachedItem(){
moveDown = false;
}
}

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

public class RopeRenderer : MonoBehaviour
{
private LineRenderer lineRenderer;

[SerializeField]
private Transform startPosition;
//Starting point for the rope

private float line_Width = 0.1f;

void Awake() {
lineRenderer = GetComponent<LineRenderer>();

lineRenderer.startWidth = line_Width;

lineRenderer.enabled = false;
}

void Start()
{

}


public void RenderLine(Vector3 endPosition, bool enableRenderer) {
if(enableRenderer)
{
if(!lineRenderer.enabled) {lineRenderer.enabled = true;}

lineRenderer.positionCount = 2;
}

else {

lineRenderer.positionCount = 0;

if(lineRenderer.enabled) {lineRenderer.enabled = false;}
}

if(lineRenderer.enabled) {

Vector3 temp = startPosition.position;
temp.z = -10f;

startPosition.position = temp;

temp = endPosition;
temp.z = 0f;

endPosition = temp;

lineRenderer.SetPosition(0, startPosition.position);
lineRenderer.SetPosition(1, endPosition);
}

}// Draw line
}

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

public class HookScripts : MonoBehaviour
{
[SerializeField]
private Transform itemHolder;

private bool itemAttached;

private HookMovement hookMovement;

private PlayerAnimation playerAnim;

void Awake() {
hookMovement = GetComponentInParent<HookMovement>();
playerAnim = GetComponentInParent<PlayerAnimation>();
}

void OnTriggerEnter2D(Collider2D target) {

if (!itemAttached && (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM
|| target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM
|| target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL || target.tag == Tags.CASH_BAG
|| target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE)){

itemAttached = true;

target.transform.parent = itemHolder;
target.transform.position = itemHolder.position;
// Set the position of the hook to the position of the item

hookMovement.move_Speed = target.GetComponent<ItemScripts>().hook_Speed;

hookMovement.HookAttachedItem();

//animate player
playerAnim.PullingItemAnimation();


if (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM
|| target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM
|| target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL || target.tag == Tags.CASH_BAG)
{

SoundManager.instance.HookGrab_Jewel();
}
else if (target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE){

SoundManager.instance.HookGrab_Stone();
}

SoundManager.instance.WinchCrank(true);

} // If the target is an item

if (target.tag == Tags.DELIVER_ITEM){

if(itemAttached){

itemAttached = false;
Transform objChild = itemHolder.GetChild(0);
objChild.parent = null;
objChild.gameObject.SetActive(false);

playerAnim.IdleAnimation();
SoundManager.instance.WinchCrank(false);
}
}// Remove items after winching

}// On trigger enter
}

This topic is closed to new replies.

Advertisement