I am not entirely sure if this is some kind of a bug or something, I did debug the script and it does give all the values correctly but this particular if statement executes when clearly it is false.
Please tell me why exactly line 37 → if (PlayerSoldier1_CurrentAP <= AP_Distance) even if I set PlayerSoldier1_CurrentAP to 1 and selected a position that returns AP_Distance of more than 1
What I want to happen is that my object doesn’t move if i clicked too far from it. Currently it goes wherever i click regardless of this if statement
void LateUpdate()
{
GameObject theGameManager = GameObject.Find("GameManager");
GameManager GameManagerScript = theGameManager.GetComponent<GameManager>();
// move player unit only if player's turn
if (GameManagerScript.CurrentState == GameManager.GameStates.PLAYERTURN)
{
if (Input.GetMouseButtonDown(0)) // check for left mouse button
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
// check if PlayerUnit 1 is selected
if (hit.collider.name == "PlayerUnit 1")
return;
// check if PlayerUnit 2 is selected
if (hit.collider.name == "PlayerUnit 2")
return;
// if PlayerUnit 1 is selected move unit
if (GameManagerScript.is_PlayerUnit_1_Selected == true)
{
GetCurrentAP();
target.position = hit.point;
Vector3 distance = PlayerSoldier1.transform.position - target.position;
AP_Distance = Mathf.FloorToInt (distance.magnitude);
Debug.Log (AP_Distance);
if (PlayerSoldier1_CurrentAP <= AP_Distance)
{
PlayerSoldierTarget1.transform.position = target.position;
PlayerSoldier1_CurrentAP--;
}
Debug.Log("Not enough AP to move");
}
// if PlayerUnit 2 is selected move unit
if (GameManagerScript.is_PlayerUnit_2_Selected == true)
{
target.position = hit.point;
PlayerSoldierTarget2.transform.position = target.position;
}
}
}
}
There’s no bugs with if, it would break thousands of programs. It’s also nothing to do with unity (mono C#) so it’s probably just a bug in your own code here. I took a look and wondered why you were rounding distance to an integer though?
i rounded it so it is not very large number when printing out debug info, and yes i know that its not really a bug with c# but there is something wrong here, and i don’t get it what causes this to execute falsely.
AP_Distance that was outside the loop gave 7 and the if loop gave 1 <= 7 and than moved the object regardless. it also printed out my ‘not enough AP to move’ before moving
1 <= 7 is true, and if will execute. Your Debug.Log(“Not enough AP to move”); is outside the If-condition where you have set it to always write, regardless of if it does the if or not. Maybe you wanted to put there a “if - else” condition?
what i wanted was that the if loop only executes if the AP_Distance is less than or equal to value of PlayerSoldier1_CurrentAP, how do i do this exactly? switching places doesn’t work, for a moment i thought that i had the thing setup in reverse.
later debug log that says ‘not enough ap’ is my fault while messing with the fixing of this thing.
PlayerSoldier1_CurrentAP >= AP_Distance?
Also, is the fact that it checks the AP against the entire distance, but only subtracts 1 from it when you do move correct?
if i do place this code it and click close to my object it goes 1<=0 and 0<=0 and than it doesn’t print out that ever again but it sill continues to move the object and print out the AP_Distance. I did try that if logic earlier today for sure, AP_Distance is the distance from object to mouse click and it returns that value always.
Turns out that target.position = hit.point was moving the object. As opossed to original code Vector3 distance = PlayerSoldier1.transform.position - hit.point; is actually how i was supposed to get the distance from object to mouse click and than have target.position = hit.point inside of my if loop to move the object.
// if PlayerUnit 1 is selected move unit
if (GameManagerScript.is_PlayerUnit_1_Selected == true)
{
GetCurrentAP();
Vector3 distance = PlayerSoldier1.transform.position - hit.point; // distance from object to mouse click
AP_Distance = Mathf.FloorToInt (distance.magnitude);
Debug.Log ("AP cost: " + AP_Distance);
if (PlayerSoldier1_CurrentAP >= AP_Distance && PlayerSoldier1_CurrentAP > 0)
{
target.position = hit.point; // this actually moves the object -> hit.point stores the coordinates of mouse click -> target.position moves the object
Debug.Log("Have enough AP to move");
PlayerSoldierTarget1.transform.position = target.position;
PlayerSoldier1_CurrentAP--;
}
if (PlayerSoldier1_CurrentAP <= AP_Distance)
{
target.position = PlayerSoldierTarget1.transform.position;
Debug.Log("Not enough AP to move");
}
}