Turn Based Movement - Using Movement Points
Hi, I am Fairly New to Coding and Unity and I have been working on this Game for a little While and I have been Stumped with this Particular Problem.
I want to Limit my Gameobjects Movement by Movement Points, so It cant move more than its Movement points, I also want it so it can move Tiny bits inside the Movement Point Range.
Answers in C# Please.
Thanks in Advance for your Answers.
my Attempt At Making it Work
public float MaxMovementPoints = 10f;
float TravelCost;
float MovementPoints;
string Selected = "Not Selected";
string Movement = "Not Moving";
List<Vector3> waypoints = new List<Vector3> ();
Vector3 mousePos;
Vector3 MouseClick;
Vector3 MouseClickFun;
// Use this for initialization
void Start ()
{
MovementPoints = MaxMovementPoints;
print ("TravelDistance is in Start Replace ASAP, add it to Refresh or something Similar");
}
// Update is called once per frame
void Update ()
{
//next turn refresh
if (Selected == "Is Selected") {
if (Input.GetMouseButtonUp (1) && Movement == "Not Moving") {
//Creating the Vector 3 of the Mouse Click
mousePos = Input.mousePosition;
mousePos.z = Camera.main.transform.position.y;
MouseClickFun = Camera.main.ScreenToWorldPoint (mousePos);
MouseClick = new Vector3 (Mathf.Round (MouseClickFun.x), 0f, Mathf.Round (MouseClickFun.z));
//Clearing the List if 'a new move Order' had been given Again
if (waypoints.Count > 0) {
waypoints.Clear ();
}
//adding to the Position to Waypoints
waypoints.Add (MouseClick);
//Setting the Movement State to "Moving"
Movement = "Moving";
//Figuring the Travel cost by Distance and Clamping it so it doesnt Go over Max (And Create a Negative with MovementPoints or Under Zero
TravelCost = Mathf.Clamp (Vector3.Distance (this.gameObject.transform.position, waypoints [0]), 0, MovementPoints);
//Applies the Travel Cost to Movement Points
MovementPoints -= TravelCost;
}
if (Movement == "Moving") {
//This Moves the GameObject to the Waypoint
transform.position = Vector3.MoveTowards (this.transform.position, waypoints [0], 20*Time.deltaTime);
//once Reached its Destination Its Ready to Recieve another Order
if (this.transform.position == waypoints [0]) {
print("I Have Reached Mouse Click");
Movement = "Not Moving";
}
}
}
}
void OnMouseUp ()
{
print (Selected);
Selected = "Is Selected";
}
would using Vector3.$$anonymous$$oveTowards help? http://docs.unity3d.com/ScriptReference/Vector3.$$anonymous$$oveTowards.html
thank you for your answer, I am Going to use Vector3.$$anonymous$$oveTowards ins$$anonymous$$d of Lerp because i think it Works better for what I want. however my Problem is Constraining the movement with movement Points, I'll update the Question it make it more clear.
you need a grid in order to use it the way you want, I'm working on something similar like Heroes of $$anonymous$$ight and $$anonymous$$agic world RTS game, and i'm using the mesh renderer with quads so each quad is a movement point.
Your answer
Follow this Question
Related Questions
Moving GameObject a specific distance in the Z direction and back again - regardless of rotation 1 Answer
Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 1 Answer
Regarding transform.position in the roll a ball tutorial 0 Answers
How to properly make an object follow the edge of another object? 0 Answers
Movement Sticking 0 Answers