Search code examples
unity-game-enginetransformpong

Object maintains changes to its Y position after getting reset


I have been programming a deeper version of Pong using skills. One of the skills is that the player will make the other player's paddle stop, and instead the enemy will start moving its goal.

I start moving the goal with this method. The method is called in the FixedUpdate.

private void moveGoal(string vertical, Vector3 nextPosition)
{
    nextPosition += speed * Time.fixedDeltaTime * Input.GetAxis(vertical) * Vector3.up;
    nextPosition.y = Mathf.Clamp(nextPosition.y, goalMinY, goalMaxY);
    rb.transform.position = nextPosition;
}

We have tried doing it in the Update and using the Time.deltaTime, but the result is the same.

We recently changed to "rb.transform.position" from "rb.MovePosition(nextPosition)" because the problem was way worse.

The position is reset with a method inside the skill's script where we have saved the base position of the goal, and once the skill gets deactivated it automatically reset the goal's position to its base position.

The problem is that if the goal starts in the Y position 1.4, after it has been reset the y position changes slightly, for example going from 1.4 to 1.25.

We do not understand why it is moving even though the position we set it to is always the same.

I am sorry if the post sounds confusing, but the problem itself is very confusing and very difficult to explain.


Solution

  • Sorry if I am misunderstanding your issue. Could it be that you reset the position of the goal but within the same frame it executes moveGoal() once, setting it slightly off your original position? Goodluck.