I am programming a game in Unity with C#
I have a ship which I needs to move towards the whirlpool (target).
The ship has to be stopped before entering the whirlpool.
I need advice as to how to implement my distToWhirlpool
and distToStopPoint
Here is the assignment I'm trying to solve:
In this task, control the movement of the hero ship around the lake and stop within a defined distance of a whirlpool.
Task Outline
Control of the ship’s movement is achieved by commanding the activation of forward and rotational thrusters and/or providing a target point given the information available on the environment. Control is required to ensure that the hero ship can move safely around the map without colliding with objects that would damage or destroy it. To demonstrate control you must accelerate your ship to a prescribed speed and maintain this speed for about 2 seconds. As the hero ship moves, the interface will give it its position in the lake and its distance from the lake’s centre. To complete the task the hero ship must move to a whirlpool and stop within a defined distance from it without being sucked in to the whirlpool.
Here is the code I have so far:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI : MonoBehaviour {
public struct Velocity2 // Structure of velocity
{
public float forward; // Forward velocity of object in m/s
public float right; // Velocity to the right of object in m/s
}
[System.Serializable]
public struct Position2 // Structure of a position
{
public float north; // meters north from centre of lake
public float east; // Meters east from centre of lake
}
public struct HeroStatus // Structure of information on the Hero's status
{
public Velocity2 velocity; // Velocity of the Hero in m/s - - not changeable
public Position2 position; // Position of the Hero - not changeable
public float angleCW; // Orientation of Hero in degrees ClockWise from North - not changeable
public float shield; // Shield energy available, range 0 - 1 - not changeable
public float Hull; // Hull integrity, range 0 - 1 - not changeable
public bool shieldActive; // True when the shield is on - not changeable
public float distFromCentre; // Hero distance from the centre of the lake - can be used to avoid lake edges - not changeable
public float maxForwardSpeed; // This reports the maximum forward speed of the Hero in m/s - not changeable
public float maxRotationSpeed; // This reports the maximum rotational speed of the Hero in degrees/sec - not changeable
public bool task; // True if Hero is in Task Mode, else Hero is in Game Mode - not changeable
public int level; // In Task mode this is the task number, in game mode this is the level number - not changeable
public int maxLives; // The maximum lives available - not changeable
public int livesLeft; // The number of lives left in the game. In Tasks it is the number of tries left for the task. - not changeable
public float taskTimeMax; // The amount of time a task has to complete the task - not changeable
public float taskTimeLeft; // During tasks this is the time remaining to complete the task. - not changeable
public float stopDistToWhirlpool; //Dist to stop from Whirlpool in Task 1 - not changeable
public float whirlpoolDiameter; // Diameter of a whirlpool
public bool inMenu; // True when the Game or Task is in a menu - not changeable
public bool useAI; // True when AI mode is active
public bool useForwardThrust; // True when forwardThrust and not forwardSpeed is use for forward/reverse motion
public float maxForwardThrust; // Maximum forward thrust available
public bool useRotateThrust; // True when rotateThrust and not forwardSpeed is use for forward/reverse motion
public float maxRotateThrust; // Maximum rotate thrust available
}
public HeroStatus heroStatus; // Hero's status - updated every frame - not changeable
public Position2[] drones; // Array of all drones - updated every frame
public Position2[] whirlpools; // Array of all Whirlpools - updated every frame
public Position2[] shieldPowerups; // Array of all Shield power ups - updated every frame
public Position2[] hullPowerups; // Array of all Hull Power ups - updated every frame
public bool useTarget = true; // If true, the target variable is used as a position to move toward,
// If False, only forwardSpeed/Thrust and rotationSpeed/Thrust is used to move the Hero
public Position2 target; // This sets the position the Hero should move toward - Uses 'forwardSpeed/Thrust' and 'rotationSpeed/Thrust' parameters to move
public float forwardSpeed = 0; // This sets the desired forward speed of the Hero in m/s - Used when useThrust is false
public float rotationSpeed = 0; // This sets the desired rotational speed of the Hero in Degrees/sec
public float forwardThrust = 0; // Thrust used to drive Hero- Used when useThrust is True
public float rotateThrust = 0; // Thrust used to rotate Hero- Used when useThrust is True
public bool turnOnShield; // When set to true turns on the Hero's shield - If the Shield is out of energy it will not turn on
public bool logData = false; // Set to true to start a data log file and reset to false to close the file.
public string logDataString = "";// Once the data log file is open, the contents of this string will be write to the data log file.
// When the data file is first opened, this string will be writen to it as the first line of date.
// So Use it to set column heading and put commas between each heading.
// Also put commas bewteen data to create a Comma Seperated Variable (CSV) file for excel.
public bool resetDataRunTime = false; // Set this to true to reset the datalogging run time that is reported in the data log to zero.
// It will be reset to false automatically.
public string debugString = ""; // The contents of this string will be writen to the screen. Toggle it on and off with the I key.
// **************************************Do Not Modify the variables above ************************************************************
//***************************************Place your variables below********************************************************************
public float thrustScale = 0.001f;
public float stopDist = 55f;
public float velScale = 4.2f;
// Used to initialise at the start of a Game Level or Task
public void StartTaskOrLevel() // This is called at the start of each Task or game level
{
forwardThrust = 0f; // Initialise motion to zero
rotationSpeed = 0f;
}
// Used to update Hero during a task or Game Level.
public void TaskOrLevelUpdate() // This is called 50 times per second during a Task or game level but not when a menu is displayed.
{
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;
}
} else
{ // Do Game
}
}
// Used to close anything at the end of a Game Level or Task
public void EndTaskOrLevel() // This is called at the end of each Task or game level
{
}
public void task1()
{
float deltaNorth, deltaEast, distToWhirlpool, distToStopPoint;
float desiredVel;
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()
{
}
}
No comments:
Post a Comment