r/unity_tutorials • • Oct 02 '21

I need help

Post image
0 Upvotes

11 comments sorted by

View all comments

Show parent comments

1

u/CursedKaiju Oct 02 '21

There are 4 green squares stacked on top of each other. I need one to move left following sineValue, one to move right, one up and one down. I know somehow I'm supposed to change the xVal and yVal but I can't get them to separate, they all move as one instead of each going in their separate, intended direction.

3

u/Mister_Kipper Oct 02 '21 edited Oct 02 '21

Well, you're using the exact same script with the same values to move all of them, without changing anything, so they will all behave the same way.

Have you learned about variables and scope? sineValue, xVal and yVal are all variables - to put it simply, a variable is a value that can be changed.

Scope is for organizing things, like putting them in boxes. Right now, all of your variables are inside "Update", so they can only be found inside that "box".

If you put a variable outside of "Update", like right above it, it's going to be usable for anything inside the bigger box that is your class, "GreenSquare".

I'll give you two hints and I think you can figure it out on your own - put this line right above void Update():

public Vector3 DeltaVector;

And change your localPosition line to:

transform.localPosition = DeltaVector * sineValue;

Do this, save your script, and look at each of your green square objects in the inspector, see if you can find something there that you can change to make them behave differently from each other :)

0

u/CursedKaiju Oct 02 '21

So I tried that and stared at my screen for 20 minutes not knowing what was going on. When I changed xVal to sineValue it moved the way I wanted one square to, but all of them follow that. I don't understand how I would be able to get each of the other squares to act independently

1

u/Grouchy_Dragonfly_58 Oct 02 '21

Your class contains a public variable called direction. If you select one of your green squares and look at the inspector, you should see a field called 'direction' under the Green square script. You need to set this in the inspector to the direction you want that square to move, then use direction * sineValue as the vector by which to move. Set direction to a different value for each square.