Quick question: Setting new vector3's

Hey guys. So, I’ve been messing around with math functions and just general math manipulation, and I’m kinda stumped on a concept that is probably really easy. Perhaps you guys can help me understand.

So, heres the full code. This is just moving an object on the Y axis by a sine wave.

    public float amplitude;
    public float frequency;
    public Vector3 tempPos = new Vector3 ();
    public Vector3 offset = new Vector3 ();
  
    void Start()
    {
        offset = transform.position;
    }

    void Update()
    {
        tempPos = offset;
        tempPos.y += Mathf.Sin (Time.fixedTime * Mathf.PI * frequency) * amplitude;
        transform.position = tempPos;
    }

specifically this part is what I’m confused on and can’t wrap my head around.

  public Vector3 tempPos = new Vector3 ();
  public Vector3 offset = new Vector3 ();

Can someone just explain to me why we are setting this as an empty Vector3 in relation to the rest of the code?

From experimenting, I can see what the difference is, but why? Perhaps its just the variable naming convention thats used… TempPos and offset. Is it appropriate to call it that in this situation in relation the what the codes doing? Sorry if this is a really newbie question lol. Funny I understand sine but not this.

So I guess, can someone just breakdown whats going on in this code? I wrote it half based on memory and half based on math logic (The actual sine wave logic)

Thanks!

They’re just initializing the 2 vector variables.

It’s actually not necessary.

1 Like

When taking out the tempPos one tho it doesnt work. I can take out the offset one tho and it seems to work. I believe their purpose is in storing values but Im not seeing the pattern here.

clarify your question. the fact that the variables are getting set to “new Vector3()” is as lordofDuct said, not necessary, because they are getting overwritten before they are used anyway. honestly it only affects what the default values will be when the monobehaviour is first created in the editor. and as soon as the game start those values can change immediately.

removing an entire variable is different from not assigning a default. That’s a completely different question and will affect the behavior of the script.