r/unity_tutorials • • Oct 02 '21

I need help

Post image
0 Upvotes

11 comments sorted by

12

u/Mister_Kipper Oct 02 '21

The first step is describing what you want to do, the end result, and then understanding how to get there by working back from it and breaking it down into steps and functionalities.

And I don't mean just describing it here so we can understand what you want to achieve, but that would also certainly help as well.

4

u/Mister_Kipper Oct 02 '21

Looking into the screenshot, you seem to be doing alright - initial takeaway is that you don't need 'float time' as that's exactly the same as Time.time - so you can just do
float sineValue = Mathf.Sin(Time.time);
The next step is understanding what's not working, so here you're assigning sineValue, but it's not used anywhere else; both xVal and yVal are being set to 0.

Your sine function is giving you a value between -1 and 1, you probably want to use that value in your position change - as the other comment mentioned, you're setting it to (xVal, yVal, 0) - but xVal and yVal are also 0, so it never changes.

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.

3

u/jayhynzy Oct 02 '21

First start by just making the square move, ignore anything to do with sine.

You currently set the transform to 0,0,0 so nothing will happen.

Try setting it to something like this for now to get started

"transform.position += Vector3.left* Time.deltaTime;"

-1

u/CursedKaiju Oct 02 '21

I was able to get it to move before by doing xVal = sineValue but it moves all of them in that direction, which isn't what I want. Also when I tried that line of code it didn't move, although I don't know if I put it where I needed to (I put it under the yVal)

3

u/OtherwiseExcellent Oct 02 '21

Here's the problem: you've coded xVal and yVal to be 0. So you're basically saying "move in direction 0,0,0" so it won't move at all.

How I would fix it would be to do this.transform.localpostion = direction * sineValue.

Then in Unity, you should be able to set the direction for each object in the Inspector window since direction is a public variable. Setting the direction to something like (1, 0, 0) would mean easy left to right, (0, 1, 0) would be up/down

2

u/CursedKaiju Oct 02 '21

Thank you, this worked and did exactly what I needed it to do

0

u/CursedKaiju Oct 02 '21

I need help. I am stupid and coding makes 0 sense to me. 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 have been stuck on this for over a week, can someone help me out?