- Home /
Move game object in opposite direction of player in x & y axis? (2D Game)
I'm working on a game mechanic where one object moves with the player 1 to 1, but another object would move in complete opposite. So if the player moves right, the object moves left. Same would be the case if the player jumped, the object would move down as the player went up towards the jumps apex.
I have it half figured out. The object that goes 1:1 with the player is working fine, but I just can't figure out how to get the second object to move in the opposite directions. When I start moving, the object flies to the top of the screen.
Here is my Code below.
private void FixedUpdate() {
if (player.isMoving)
{
if (isRed)
{
targetPosition = player.transform.position + startPosition; // Red Box (Works Great)
transform.position = targetPosition;
}
else
{
targetPosition = player.transform.position + startPosition; // Blue Box (Broken)
transform.position = -targetPosition;
}
}
else
{
startPosition = transform.position - player.transform.position; // Return to resting position
transform.position = player.transform.position + startPosition;
}
}
I think the object flying to the top when I start moving may have something to do with it getting the -Y position from the -targetposition in the else{} clause. I have no clue where to go from here. Anyone out there have an idea on how to solve this problem?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Having trouble moving object in opposite direction of player. 1 Answer
Distribute terrain in zones 3 Answers
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer
The player randomly freezes in place while other objects move ingame 1 Answer