0
\$\begingroup\$

I'm trying to make a game object follow another game object and switch to another when a button is pressed. But right now it just seems to change the position when the button is pressed but it doesn't follow the object. How can I make it follow the other object?

public Transform PosObjOne;

public Transform PosObjTwo;

public Transform PosObjThree;

void Update () {
    if (Input.GetButtonDown("UseVr"))
    {
        PosObjThree.position = PosObjTwo.position;
    }

    if (Input.GetButtonDown("NoVr"))
    {
        PosObjThree.position = (PosObjOne.position);
    }
}

\$\endgroup\$
0
\$\begingroup\$

Well right now the position only gets set when you press the button, so there is no continuous loop that is setting the position after pressing your button once.

To keep the object at the other object's position you could make use of a boolean that is set to true once you press the button, and revert back when you press the other button again. Then in na if statement check the boolean's value and set the correct position.

For example:

public Transform PosObjOne; 
public Transform PosObjTwo; 
public Transform PosObjThree; 
bool FollowOne = true;

void Update () { 
    if (Input.GetButtonDown("UseVr")) { 
        FollowOne = false;
    } 

    if (Input.GetButtonDown("NoVr")) { 
        FollowOne = true
    } 

    if(FollowOne) {
        PosObjThree.position = PosObjOne.position; 
    } else {
        PosObjThree.position = PosObjTwo.position; 
    } 
}
\$\endgroup\$
0
\$\begingroup\$

There is a little problem with the code. When you press the button "UseVR" PlayerThree does follow PlayerTwo but only for as long as the button is pressed. As you stop pressing the button, the if statement is false, and it stops following PlayerTwo, and same happens with the follow PlayerOne's if statement. So we need to insert a concept that follows the expected Player even after the button is pressed once. So making a bool variable, childOf_Two will solve the problem.

Declare the variable childOf_Two after declaration of three Transform variables.

private bool childOf_Two; void Update () { if (Input.GetButtonDown("UseVr")) { childOf_Two = true; } if (Input.GetButtonDown("NoVr")) { childOf_Two = false; } }

Now we know that childOf_Two will be true just after the button is pressed, and will be true for as long as you do not press another button. So now we want our PlayerOne to follow PlayerTwo after childOf_Two is true. So we will say that,

if (childOf_two == true) { PosObjThree.position = PosObjTwo.position; } if (childOf_two == false) { PosObjThree.position = PosObjOne.position; }

We will write this inside Update() after the previous code. So now it will follow PlayerTwo as long as the value of childOf_Two stays true and will be same for PlayerOne.

Hope this helped.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.