- Home /
Why can't OnTriggerEnter2D not change negative value?
I'm referencing another script inside a Unity event to change the direction of the player in-game.
The problem is that my script changes the x value to a negative float but it doesn't change it back to positive even though it can successfully check for a negative value.
I wasn't sure if I needed another collision check but why can it change the x value from positive to negative but not from negative to positive. Any help is appreciated.
public void OnTriggerStay2D(Collider2D collider)
{
var autocroll = collider.gameObject.GetComponent<Autoscroll>();
float myDirection = autocroll.directionForce.x;
if(collider.gameObject.tag == "Player")
{
if (myDirection < 0) //Moving left
{
myDirection = -5f; //Turn right
print("moving left");
}
if(myDirection > 0) //Moving right
{
myDirection = 5f; //Turn left
}
}
}
Answer by JacquesVisser · Jul 29, 2019 at 12:42 PM
Instead of using two if statements I just cleaned up the code to set it as a negative value of itself each time it enters a specific collider.
public void OnTriggerEnter2D(Collider2D collider)
{
var autoscroll = collider.gameObject.GetComponent<Autoscroll>();
if(collider.gameObject.tag == "Player")
{
autoscroll.directionForce.x = autoscroll.directionForce.x * -Mathf.Abs(1);
}
}
Answer by xxmariofer · Jul 26, 2019 at 10:15 PM
Because myDirection isnt a pointer/reference, is a copy. in myDirection you are not saving the reference to autcroll.directionForce.x but just the value.
float valuaA = 0;
float valueB = 0;
//-------------------both floats are 0
valuaA = 5;
valueB = valuaA;
//------------------both floats are 5
valueB++;
//-----------------valuaA = 5 and valueB = 6
so to fix this simply change the property value
autocroll.directionForce.x = 5;//and the same with -5
Unfortunately, even with the reference set, the value does not change back to positive, thanks for pointing out the typo.
public void OnTriggerStay2D(Collider2D collider)
{
var autoscroll = collider.gameObject.GetComponent<Autoscroll>();
if(collider.gameObject.tag == "Player")
{
if (autoscroll.directionForce.x < 0) //$$anonymous$$oving left
{
autoscroll.directionForce.x = -5f; //Turn right
}
if(autoscroll.directionForce.x > 0) //$$anonymous$$oving right
{
autoscroll.directionForce.x = 5f; //Turn left
}
}
}
Does it have to from the variable type it's referencing?
public Vector2 directionForce;
In the editor, the player object simply passes through colliders, so I doubt there's anything locking the value there.
pretty sure you wanted to put the 5 where is the -5 and the -5 where is the 5
if (autoscroll.directionForce.x < 0) //$$anonymous$$oving left
{
autoscroll.directionForce.x = 5f; //Turn right
}
if(autoscroll.directionForce.x > 0) //$$anonymous$$oving right
{
autoscroll.directionForce.x = -5f; //Turn left
}
Your answer
Follow this Question
Related Questions
2D triggers not working 2 Answers
"Private member OnTriggerEnter2D is unused"? 0 Answers
What is the difference between these two scripts? 1 Answer
why OnTriggerEnter2d runs four-time? 0 Answers