Trouble trying to compare OLD against NEW values?

The need:

I need to compare the previous direction with the new direction (normalized vectors)

The problem:

The old and new values are always exacly the same, cant get the old values. The variable holding the value is only update AFTER i Debug both values, and still the values are the same.

Is there some lifecycle of unity I’m not understanding at this point?


void CheckMovementInteraction() {
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    Vector3 newDir = new Vector3(x, 0, z).normalized;
    axisAbsDisplacement = Mathf.Abs(newDir.x) + Mathf.Abs(newDir.z); // PRIVATE VARIABLE
    isMoving = Mathf.Clamp01(axisAbsDisplacement) > 0; // PRIVATE VARIABLE

    Debug.Log("OLD DIRECTION: " + axisNormalizedDirection);
    Debug.Log("NEW DIRECTION: " + newDir);

    axisNormalizedDirection = newDir; // PRIVATE VARIABLE
}

5 Answers

5

The console you have shown seems to have the “Collapse” option enabled. This will group together all messages that are exactly the same and will give you a count of the number of times that message occurred. This is the number on the right column. This can be useful in some cases but you may no longer see the order of messages properly.

Can you try disabling Collapse and see if you see the correct order of OLD DIRECTION and NEW DIRECTION messages?

image

@dstears oh ok! I’ll try to see that, sorry

@dstears That would not be the case since my two logs have different strings, i did that just for that reason you just mentioned, the two values will never be grouped.

But in order to see the value of axisNormalizedDirection and newDir in a single call of CheckMovementInteraction() you need to look at the log in the un-collapsed view. You are claiming that axisNormalizedDirection is always equal to newDir but that is impossible to determine from the collapsed log. You could also just explicitly Debug.Log the value (newDir == axisNormalizedDirection) to see if this ever prints false.

Your code seems to be working. You’re printing stuff to the console every frame, which makes it hard to tell what is happening.
Try replacing your Debug.Logs with this:

if (newDir != axisNormalizedDirection) {			
	Debug.Log ("direction changed");
	Debug.Log ("OLD DIRECTION: " + axisNormalizedDirection);
	Debug.Log ("NEW DIRECTION: " + newDir);
}

Now, what are you REALLY trying to do here?
If you wanted to keep track of the last type of movement the player did, which is different from the current type of movement, you need to do something different:

var input = new Vector3 (x, 0, z).normalized;//Store input into a temp variable first
if (input != newDir) {
	axisNormalizedDirection = newDir; //Only assign the current direction to the last direction, if something changed
}
newDir = input;
//...
//Comment out this line:
//axisNormalizedDirection = newDir; // PRIVATE VARIABLE

This operates differently than your code. Say your player is not moving, then starts moving continuously, the lastMovement (axisNormalizedDirection ) will be 0,0,0 and the new movement (newDir) would be something like 1,0,0. The lastMovement will stay at 0,0,0 until the player makes a move which is different than 1,0,0

@ArachnidAnimal The goal here is to check the dot product between the old and new values, and only execute an action if both directions are completely opposite from the other, wich means the player is turning backwards.