0
\$\begingroup\$

I am typing a code using C# in unity for game called Sea Gauntlet. In this game, there is a hero ship which I am suppose to control to do certain tasks. Along with that there is whirlpools, drones designed to destroy the ship and there are shield and hull power ups to survive in this game. It is surrounded by lake.

There are different tasks which needs to be done. I have managed to do these following tasks:- 1. In this task your agent must control the movement of the hero ship around the lake and stop within a defined distance of a whirlpool. 2.In this task your agent will need to pick up a Hull Power up and a Shield Power up.

While I am trying to do the task:- In this task you will be required to collect 6 powerup (shield and hull powerup). I do not understand as to how to approach towards this task? Can someone help with this challenge?

I have done the coding so far till here.

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

public class AI : MonoBehaviour 
{

    public float thrustScale = 0.001f;
    public float stopDist = 55f;
    public float velScale = 4.2f;
    public void TaskOrLevelUpdate() // This is called 50 times per second during a Task or game level but not when a menu is displayed.
    {
        //Display ship position
        debugString = "North: " + heroStatus.position.north.ToString() + "\nEast: " + heroStatus.position.east.ToString();
        if (heroStatus.task)
        {                   // Do Tasks
            switch (heroStatus.level)
            {
                case 1:
                    task1();
                    break;
                case 2:
                    task2();
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 10:
                    task10();
                    break;
            }
        } else
        {                   // Do Game

        }
    }
    public void task1()
    {
        float deltaNorth, deltaEast, distToWhirlpool, distToStopPoint;
        float desiredVel;

        if (whirlpools.Length == 0) // If no whirlpools then finish the method
            return;
        deltaNorth = whirlpools[0].north - heroStatus.position.north;
        deltaEast = whirlpools[0].east - heroStatus.position.east;
        distToWhirlpool = Mathf.Sqrt(Mathf.Pow(deltaNorth, 2) + Mathf.Pow(deltaEast, 2));
        distToStopPoint = distToWhirlpool - heroStatus.stopDistToWhirlpool;

        target = whirlpools[0];
        useTarget = true;

        desiredVel = distToStopPoint * velScale;
        if (distToStopPoint < stopDist)
        {
            forwardThrust = heroStatus.maxForwardThrust * -desiredVel * thrustScale;
        }
        else
        {
            forwardThrust = heroStatus.maxForwardThrust;
        }
        rotationSpeed = heroStatus.maxRotationSpeed;
    }

    public void task2()
    {
        if (shieldPowerups.Length == 0 && hullPowerups.Length == 0)
            return;
        useTarget = true;
        forwardThrust = heroStatus.maxForwardThrust;
        rotationSpeed = heroStatus.maxRotationSpeed;
        if (shieldPowerups.Length!=0 && hullPowerups.Length!=0)
        {   //There is both shield and hull power up
            float distShield, distHull;

            distShield = DistBewteenPoints(heroStatus.position, shieldPowerups[0]);
            distHull = DistBewteenPoints(heroStatus.position, hullPowerups[0]);

            if (distShield < distHull)
                target = shieldPowerups[0];
            else
                target = hullPowerups[0];

        }
        else  // There is only one shield or hull power up
        {
            if (shieldPowerups.Length != 0)
                target = shieldPowerups[0];
            else
                target = hullPowerups[0];
        }
    }

    public void task10()
    {
        // Only one drone in task 10 as we have set it up
        if (drones.Length == 0)
            return;

        Position2 offset, droneLocal;

        offset = heroStatus.position;
        // Convert to local
        droneLocal.north = drones[0].north - offset.north;
        droneLocal.east = drones[0].east - offset.east;
        // get the flee drone position
        droneLocal.north *= -1f;
        droneLocal.east *= -1f;
        // Convert back to Golbal
        droneLocal.north = droneLocal.north + offset.north;
        droneLocal.east = droneLocal.east + offset.east;

        useTarget = true;
        target = droneLocal;
        forwardThrust = heroStatus.maxForwardThrust;
        rotationSpeed = heroStatus.maxRotationSpeed;
        debugString = "Flee Pos: " + droneLocal.north.ToString() + " , " + droneLocal.east.ToString();
    }


    public float DistBewteenPoints(Position2 point1, Position2 point2)
    {
        float dist;

        dist = Mathf.Sqrt(Mathf.Pow(point2.north - point1.north, 2) + Mathf.Pow(point2.east-point1.east, 2));

        return dist;
    }
\$\endgroup\$
  • \$\begingroup\$ Make sure your code is shown correctly, it's separated right now. I'm not really sure what your task is. Are you supposed to add items in a scene, which the ship is able to pickup? In that case you'd need to make them their own gameobject and add collsion detection. Upon collision you would delete the object (or despawn it, if you're using object pooling) and give the ship their respective special attributes. \$\endgroup\$ – iQew May 8 at 9:03
  • \$\begingroup\$ Actually the items that is shield and hull power ups, whirlpools and drones are already there in it. I needed to know that how to approach the problem of getting ship move to collect both power ups 3 times in an alternate order. \$\endgroup\$ – Omega_06 May 8 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.