Anyone could take a look at my notes.. make sure i'm not miss understanding anything

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]                                  // Only one Scripts
public class Xfactor : MonoBehaviour {

    [SerializeField] Vector3 movementVector = new Vector3(5f, 5f, 5f);
    [SerializeField] float period = 2f;                     // the full period of a circle is 2(f) secs

    /*  remove from inspector later
    2 mods effecting float (range is awesome, gives you slider). moment 0 = no movement 1 = complete movement */
    [Range(0,1)][SerializeField] float movementFactor;         

    Vector3 startingPos; 

    void Start () {
        startingPos = transform.position;                    // the start of moving object
	}
	
	// VERY IMPORTANT FOR AI MOVEMENT!!!! Read carefully
	void Update () {
        float cycles = Time.time / period;                  //divides times the game cycle. smoothes it per frame
        const float tau = Mathf.PI * 2f;         //     *****equals to about 6.28. look up tau for more info*****
        float rawSinWave = Mathf.Sin(cycles * tau);

        movementFactor = rawSinWave / 2f + .5f;             // *****Crops movement and speeds or slows down obj******
        Vector3 offset = movementVector * movementFactor;  // displacement. multi scale vectors
        transform.position = startingPos + offset;          // add starting plus the offset.
	}
}

I always had a problem understand this… I would just copy and paste code before without trying to understand it bc i had no one to explain it to me! lol let me know if my notes are dead on or if i miss understood something. Thank you in advance

Looks like you’ve got the gist of it from your comments. Well done. It’s a good idea to add your own comments to code that’s given to you, especially because there will come a time later when you’ll go back to that code and be like “what was that code doing again?” Even those of us who have been programming a LONG time need to comment our work liberally.

Thank you. Reassuring just having someone else go through it.

Privacy & Terms