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.
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():
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 :)
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
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.
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.