How to make a GameObject move opposite to the player's movements?

I want to have a GameObject that mirrors the player’s movements. For example, if the player jumps to the right, then the GameObject will ‘jump’ to the left the same distance that the player jumped (so everything is equal).

  • create a script attached to the player that stores its initial position
  • every update calculate and store an offset from that initial position
  • create and attach a script to the mirrored object that stores its initial position
  • in the mirrored object script, every update read the player’s offset and negate x and z - set the transform.position equal to the mirrored objects initial position plus this offset
  • optionally copy the animations
  • loop through all of the AnimationStates in the player.animation, make your loop variable animstate
  • find the equivalent state on the mirror using mirror.animation[animstate.name] and copy the rest of the values across (enabled, weight, speed, time, layer etc)

using UnityEngine;
using System.Collections;

public class Mirror : MonoBehaviour {

    public GameObject mirrorObject; // Object to mirror
    public bool mirrorX; // Wether or not to mirror the X axis
    public bool mirrorY; // Wether or not to mirror the Y axis
    public bool mirrorZ; // Wether or not to mirror the Z axis

    public bool useLateUpdate; // Wether or not to set the object's position in LateUpdate

    void Update()
    {
	    Vector3 newPosition;
	    newPosition = mirrorObject.transform.position; // Get the mirrorObject's position
	
	    if(mirrorX)
	    {
		    newPosition.x = -newPosition.x; // Mirror on the X axis
	    }
	    if(mirrorY)
	    {
		    newPosition.y = -newPosition.y; // Mirror on the Y axis
	    }
	    if(mirrorZ)
	    {
		    newPosition.y = -newPosition.y; // Mirror on the Z axis
	    }
	
	    if(!useLateUpdate)
		    transform.position = newPosition; // Set object's position equal to the new mirrored position
    }

    void LateUpdate()
    {
	    if(useLateUpdate)
		    transform.position = newPosition; // Set object's position equal to the new mirrored position
    }
}

Here you go decided to go ahead an type this one up for yah :slight_smile: