- Home /
Variable value not changing (bug or i'm just an idiot?)
Hey everyone. First of all, this is actually my first time sharing a problem with you guys (if im asking you for help is because i really need your master knowledgement).
The problem is simple, but complex for explainning it to you, but ill try my best.
Im making an RTS game like age of empires and so i have workers, which can build, harvest resources, fight and move. Ive made a class for each action (Move, Harvest, Build, Fight).
The problem is in the class Harvest. In here, the worker needs to move to the resource object and then gather a fixed amount of resource. The problem is that the worker cant actually start moving to the object (i use the Move Class to help me do the movement).
When i started making some Debug.Log(), i noticed that the variable (boolean) moving doesnt go from False to True (it actually goes to True, but in the method performAction() it stays False).
Here you have the code for the Move Class:
public class Move : Action {
private Unit origin;
private Vector3 destinationPos;
private Quaternion destinationRot;
private bool rotating, moving;
private float moveSpeed, rotateSpeed;
public Move(Unit origin, WorldObject destination) {
this.origin = origin;
this.destinationPos = destination.transform.position;
StartMove(destinationPos);
}
public Move(Unit origin, Vector3 destinationPos) {
this.origin = origin;
this.destinationPos = destinationPos;
StartMove(destinationPos);
}
public override void PerformAction() {
if(rotating) {
TurnToTarget();
}
else if(moving) {
MoveToTarget();
}
}
protected void MoveToTarget() {
origin.transform.position = Vector3.MoveTowards(origin.transform.position, destinationPos, moveSpeed);
if(origin.transform.position == destinationPos || origin.CollidedObject) {
moving = false;
origin.transform.position -= new Vector3(0f,0f,-2f);
origin.CollidedObject = null;
}
}
private void TurnToTarget() {
origin.transform.rotation = Quaternion.RotateTowards(origin.transform.rotation, destinationRot, rotateSpeed);
//Faz o caminho mais rapido na rotacao e retira o problema de o unit ficar virado ao contrario
Quaternion inverseTargetRotation = new Quaternion(-destinationRot.x, -destinationRot.y, -destinationRot.z, -destinationRot.w);
if(origin.transform.rotation == destinationRot || origin.transform.rotation == inverseTargetRotation) {
rotating = false;
moving = true;
}
}
private void StartMove(Vector3 destination) {
moveSpeed = origin.moveSpeed;
rotateSpeed = origin.rotateSpeed;
//Calcula o movimento (rotacao e tranlaccao) entre o destino e a origem
destinationRot = Quaternion.LookRotation(destination - origin.transform.position);
rotating = true;
moving = false;
}
}
I really hope theres no need for me to write the harvest class in here (its a bit big).
In GENERAL, the question is: Why the value of moving changes to True in one method (TurnToTarget()) but not in the other (PerformAction())??
Thanks for everything
Pedro Gomes
PS: The Class Move works fine when its used in the Unit, but NOT in the Harvest.
Do you enter in the statement :
if(origin.transform.position == destinationPos || origin.CollidedObject) {
of the $$anonymous$$oveToTarget function ? Put a breakpoint or a log to be sure you go in there.
Do you call PerformAction
once? If yes, then TurnToTarget
method is called (assu$$anonymous$$g rotating is true). Inside that method, moving is set to true, and then method returns. But
else if(moving)
statement won't be checked, because it is a part of a previous if
statement, which was already tested and succeeded. If you want to check for moving then, depending on what you want to achieve, you either have to call PerformAction
again or use if(moving)
ins$$anonymous$$d of else if(moving)
.
$$anonymous$$iraSensei, i did check that, but it wont go to that statement.
AkaneX you are actually right... If i dont have that statement (if(moving)), the unit cant move... But that doesnt seem right.. the PerformAction() from the Harvest (where it has the PerformAction() from the $$anonymous$$ove) is called in the Update(), so that means that the method is always being called.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142750im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# How To Save PlayerPrefs for Unity GUI 2 Answers
C# Custom Class - Declare Variable's Value 1 Answer
Decreasing a value as another value increases through script 4 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers