How to mirror the behavior of an object

Hello,

I just created this very basic scripts that lets a cube spawn into a different location in the opposite side whenever it reaches a certain location, it’s working fine, the problem I’m facing is that I wanted to do the same thing for a different object but the behavior should be mirrored for the opposite side, i tried using the same original script i created with updated names and locations to match the new object but it doesn’t work
Any idea why this is not working guys? Any help or guidance will be much appreciated,I’m a 3d artist and this is my first attempt into coding in unity
Thank you so much for ready and your time

Here is the original script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class leftdirection : MonoBehaviour {

public float sidewayspeed = 10f;
public Transform player;
public Vector3 leftlimit ;
public Vector3 rightlimit;

void Start()
{

}
// Update is called once per frame
void FixedUpdate () {

if (Input.GetKey(“left”))
gameObject.transform.Translate(-sidewayspeed * Time.deltaTime, 0, 0);

if (transform.position == leftlimit)
transform.position = rightlimit;

}
}

and this is the mirrored script that is not working :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rightdirection : MonoBehaviour {

public float sidewayspeed = 10f;
public Transform player;
public Vector3 leftlimit ;
public Vector3 rightlimit;

void Start()
{
}
// Update is called once per frame
void FixedUpdate () {

if (Input.GetKey(“right”))
gameObject.transform.Translate(sidewayspeed * Time.deltaTime, 0, 0);

if (transform.position == rightlimit)
transform.position = leftlimit;
}
}

The odds of your position being exactly equal to the limit are basically nil, so I’m surprised the first script is working.

Instead, check if the value is greater than or equal to to your limit.

if(transform.position.x >= rightlimit.x)
    transform.position = leftlimit;
1 Like

Thank you for your reply and your time,
Believe me I wasn’t expecting it to work until it happened
I’m going to try that ,thank you again

Thank you so much, It worked !!! :slight_smile:

1 Like