1
votes

I'm new to Unity, sorry in advance if this is question is basic, but I try'ed to look online for similar questions with avail of answers. I have a gameobject which has a forward movement, when a variable is set to true I want that gameobject to move back X amount of units, but when the set back false I want the gameObject tot return to that previous movement. What I coded makes the gameObject move forward, but when triggered by the variable it does not move back. Can you please tell me what I did wrong?

public MonsterTarget monTarget;
public BandageTarget banTarget;

public Transform target;
public float speed;
public float backSpeed;

public bool back;


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    back = monTarget.targetMove;

    if (target.position.x > -11)
    {
        target.Translate(speed, 0, 0);

        if (back == true)
        {
            Debug.Log("MOVE BACK");
            target.Translate(backSpeed, 0, 0);
        }
        monTarget.targetMove = false;
    }

}
1

1 Answers

0
votes

I know in C# that a bool's default value is false, are you setting it true somewhere for the if ( back == true ) statement to begin?

Also, your target.Translate( speed, 0, 0 ); is running while your Target.Translate ( backspeed, 0 , 0 ); is running, possible canceling out each other. Make sure your variables are assigned and maybe try this.

if ( back == true) {
    target.Translate( backspeed , 0 , 0 );
} else {
    target.Translate( speed, 0 , 0 )
}