GameObject follow canvas image position

Viewed 20

I have a gameObject that I would like to move between X=-2 and X=+2. It follows the position of a canvas Image that also moves in X axis as X=12 and X=150. How do I make sure my gameObject follows the right axis of the image without parenting it? It should be clamped between -2 & +2. So how do I do this?

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

public class FollowImagePosition : MonoBehaviour
{
    public GameObject followImage;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void LateUpdate()
    {
            this.transform.position = new Vector3(followImage.transform.position.x, this.transform.position.z, this.transform.position.z);
        
    }
}
0 Answers
Related