I have 2 moving objects with basic movement in one direction:
if(CircleMovement1.switchMovement1 == false)
{
if(scoreNumber.scoreCounter >= 0 && scoreNumber.scoreCounter < 10){
speed = 0.3f;
rb.velocity = new Vector2(speed * 1.7f, 0);
}
}
On success, which is on click type of event, objects have to stop moving in that direction and return to a starting position in opposite direction having speed component a bit bigger than while they were moving in the beginning direction. On click event simply changes the boolean value of "CircleMovement1.switchMovement1" so that the objects can move in the opposite direction:
else if(CircleMovement1.switchMovement1 == true)
{
if(scoreNumber.scoreCounter >= 0 && scoreNumber.scoreCounter < 10){
rb.velocity = new Vector2(-speed * 25f, 0);
}}
The problem is: once the objects reach the starting position again they have to stop and move forward again as if CircleMovement1.switchMovement1 == false as shown in the first snippet. I don't know how to mark a moment in which they reached the starting point and can begin to move in the opposite direction. I tried adding colliders and detect a collision with an object on the starting point:
public void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == "mainCol")
{
CircleMovement1.switchMovement1 = false;
}
}
But that did not turn well because on higher speed objects sometimes penetrate through collider of the starting point and go backwards more than needed (it's not consistent).
I tried to track the local position of objects like this:
if(this.GetComponent<RectTransform>().localPosition.x == 30) {
CircleMovement1.switchMovement1 = false;
}
but that didn't work at all.
And last, I tried to use Lerp function to move objects to a starting position like this:
circ3.GetComponent<RectTransform>().localPosition = new Vector3(Mathf.Lerp(currPos3.x,-1,1f * Time.deltaTime),2,0);
This works but not in the way I want it to work. It just instantly spawns an object in the given vector position and doesn't have the same look as movement with speed.
If anyone knows how to detect the point where objects reach the position that they need feel free to help.
Thanks in advance.