Home > Software engineering >  How to make a gameObject Follow another game object on the x axis only?
How to make a gameObject Follow another game object on the x axis only?

Time:05-28

I want to figure out how to make this gameobject follow another game object on the x axis only. here is all I have so far.

public Transform mouse; 
private Vector3 offset; 

// Start is called before the first frame update
void Start()
{
    
}

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


void follow()
{
    transform.position.x = mouse.position.x;
}

CodePudding user response:

You almost had it! I might do a bad job explaining this but the reason that doesn't work is because a vector3 has readonly properties, to change it you have to create a new one, so there may be a better way but here's what I always used.

new Vector3(mouse.x, transform.position.y, transform.position.z)

Cheers

  • Related