I really appreciate these comments. It makes sense to me even more now as to WHY having the end goal of my game is very much needed at the beginning of the development.
I am just following along with the class and guessing is not a good idea… I am a firm believer that a good plan is what is needed at the start. That story line…
Each of these classes teaches me new ways to be ABLE to use the unity tools and C sharp together.
Not to mention How to use asset packs is tricky as well…
Thanks for helping me see other ideas!
Tell me what you guys think about using asset packs.
Currently what i have been doing now is this.
Drag the asset from the asset pack into the scene.
Right click it and unpack the prefab.
drag into a folder for my prefabs. (to disconnect the connection from asset pack)
Seems like asset packs are built in so many different ways. (I wish there is a section on asset packs)
Again thanks for helping me understand the basics.
SIDE NOTE:
I have been building a object move script that I can use in many of my games.
Maybe you can take a look and tell me what you think?
The idea I have is to be able to use this script to move objects in what ever game.
so far I have used it in many of these classes simply because it moves objects in so many ways.
Trying to fine tune it and hope to hear comments about it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameObjectsCanMove : MonoBehaviour
{
#region LOTS OF VARS
[Header("------------------------------------------------------------------------")]
[Header(" control the speed with the period tuner (uses the Mathf.Sin(cycles * tau))")]
[Header(" any distance on any vector where you define the period")]
[Header("Oscillator -- move [back and forth]")]
[Header("-----------------------------------------------------------------------")]
[Header("Basics")]
[SerializeField] Vector3 oscillatorMovementVector;
[SerializeField] float oscillatorPeriod = 2f;
Vector3 oscillatorStartingPosition;
float oscillatorMovementFactor;
const float tau = Mathf.PI * 2;
[Header(" Set this to true to use the waypoints INSTEAD of the Oscillator")]
[Header("------ Waypoint begin ------------------------------------------------")]
[SerializeField] bool moveAlongWaypoints = true;
[Header("------------------------------------------------------------------------")]
Transform objectToMove;
[SerializeField] float speedToMoveWaypoints = 5f;
[Header(" ---> Time to linger <---")]
[SerializeField] float moveDelayWaypoints = 1f;
[Header("------------------------------------------------------------------------")]
[Header(" Use this section for giving paths to objects")]
[Header(" Create an empty gameOject with waypoints as its children")]
[Header(" Don't forget to place your waypoints!!")]
[Header("----------------------------- WAYPOINTS ---------------------------------")]
[SerializeField] List<Transform> waypoints = new List<Transform>();
[Header(" ------------------------")]
[Header(" *** You can mix it up and use Random Waypoints *** ")]
[Header("------------------------------------------------------------------------")]
[SerializeField] bool useRandomWaypoint = false;
private int currentWaypointIdx = 0;
[Range(0.1f,1f)]
[Tooltip("Choose a distance that is close enough to call it good! .. move to the next waypoint if within this distance of waypoint")]
[SerializeField] float distanceOfWaypointIsCloseEnough = .2f;
[Header("------------------------------------------------------------------------")]
[Header(" Change the Speed every N-TH time it hits a waypoint -- for variety")]
[Header(" Change movement speed every n-th time a waypoint is hit for one trip")]
[Header("------------------------------------------------------------------------")]
[SerializeField] bool useEveryNthTime = false;
[SerializeField] int numberForEveryNthTime = 4;
[SerializeField] float multiplySpeedBy = 3f;
public int hitTargetCounter = 0;
private int rndHit = 0;
[Header("-----------------------------------------------------")]
[Header(" Rotatation")]
[Header(" Rotate the object on X, or Y, or Both")]
[Header("-----------------------------------------------------")]
[SerializeField] bool rotateTheX = false;
[SerializeField] bool rotateTheY = false;
[SerializeField] bool rotateTheZ = false;
[SerializeField] bool rotateTheOpposite;
[SerializeField] float speedToRotate = 5f;
[SerializeField] bool rotateAroundTarget = false;
[SerializeField] GameObject targetToRotateAround;
[SerializeField] Vector3 axis;
[SerializeField] float degreesPerSecondToRotate = 20f;
[Header("-----------------------------------------------------")]
[Header(" Scale")]
[Header(" Scale the object on X, or Y, or Z")]
[Header("-----------------------------------------------------")]
[SerializeField] bool scaleTheX = false;
[SerializeField] bool scaleTheY = false;
[SerializeField] bool scaleTheZ = false;
[SerializeField] float speedToScale = 5f;
[SerializeField] Vector3 scaleIncrement = new Vector3(0.1f, 0.1f, 0.1f);
[SerializeField] Vector3 scaleMax = new Vector3(10.0f, 10.0f, 10.0f);
private float scaleXStart;
private float scaleYStart;
private float scaleZStart;
private float scaleXCurrent;
private float scaleYCurrent;
private float scaleZCurrent;
private Vector3 scaleChange;
private float timeLastScaled;
[Header("Start and End of route")]
private bool firstMove;
private int direction = 1;
//Dropper
[Header("------------------------------ Dropper -----------------------------------------")]
[Header(" Set timeBetween, min and max for the X and Z, from Y will be your constant")]
[Header(" Drop Object(s) objects drop from the Y value")]
[Header("-----------------------------------------------------------------------------------")]
[SerializeField] bool dropObject = false;
[Tooltip("Choose from your prefabs.. Create one if needed, any object with Rigidbody will work")]
[SerializeField] GameObject myChildObjectPrefabToDrop;
[SerializeField] float timeBetweenDrops = 3f;
public int numberOfObjectsDropped = 0;
private float timeLastDropped;
[SerializeField] float minXvalue = -50f;
[SerializeField] float maxXvalue = 50f;
[SerializeField] float minZvalue = -50f;
[SerializeField] float maxZvalue = 50f;
[SerializeField] float fromYvalue = 10f;
[Header("Clean up work")]
[SerializeField] float timeToDestroyAfterDrop = 5f;
#endregion
void Start()
{
objectToMove = this.transform;
oscillatorStartingPosition = objectToMove.position;
firstMove = true;
scaleXStart = transform.localScale.x;
scaleYStart = transform.localScale.y;
scaleZStart = transform.localScale.z;
scaleXCurrent = scaleXStart;
scaleYCurrent = scaleYStart;
scaleZCurrent = scaleZStart;
timeLastScaled = Time.time;
//dropObject = false;
timeLastDropped = Time.time;
}
void Update()
{
FollowWaypoints();
Oscillator();
RotateAroundTarget();
RotateIt();
ScaleIt();
DropObjectsWithinYourRange();
}
void Oscillator()
{
if(moveAlongWaypoints) { return; }
if(oscillatorPeriod <= Mathf.Epsilon) { return; }
float cycles = Time.time / oscillatorPeriod; //continually growing over time
float rawSinWave = Mathf.Sin(cycles * tau); //minus one to plus 1 or -1 to +1 will be the result of .Sin
//recalculated to go from 0 to 1 so we can go from one point to another and NOT be in the center
oscillatorMovementFactor = (rawSinWave + 1f) /2f;
Vector3 offset = oscillatorMovementVector * oscillatorMovementFactor;
transform.position = oscillatorStartingPosition + offset;
}
void RotateAroundTarget()
{
if(!rotateAroundTarget) { return; }
objectToMove.RotateAround(targetToRotateAround.transform.position , axis, degreesPerSecondToRotate * Time.deltaTime);
}
void ScaleIt()
{
//Scale the x first
if(scaleTheX)
{
//scaleXCurrent += scaleXIncrement;
scaleXCurrent += scaleIncrement.x;
if(scaleXCurrent > scaleMax.x)
{
scaleXCurrent = scaleXStart;
}
}
if(scaleTheY)
{
scaleYCurrent += scaleIncrement.y;
if(scaleYCurrent > scaleMax.y)
{
scaleYCurrent = scaleYStart;
}
}
if(scaleTheZ)
{
scaleZCurrent += scaleIncrement.z;
if(scaleZCurrent > scaleMax.z)
{
scaleZCurrent = scaleZStart;
}
}
if(scaleTheX || scaleTheY || scaleTheZ)
{
//scaleChange = new Vector3(scaleXCurrent, scaleYCurrent, scaleZCurrent);
if(Time.time > timeLastScaled + speedToScale)
{
//Debug.Log("scale x is: " + scaleXCurrent + " scale y is: " + scaleYCurrent + " scale z is: " + scaleZCurrent);
StartCoroutine(ScaleItOverTime());
timeLastScaled = Time.time;
}
}
}
IEnumerator ScaleItOverTime()
{
yield return new WaitForSeconds(speedToScale);
scaleChange = new Vector3(scaleXCurrent, scaleYCurrent, scaleZCurrent);
objectToMove.localScale = scaleChange;
}
private void RotateIt()
{
float useNthSpeed = 1;
if(useEveryNthTime)
useNthSpeed = speedToRotate * multiplySpeedBy;
else useNthSpeed = speedToRotate;
if(rotateTheOpposite) useNthSpeed *= -1;
//Debug.Log("Rotate?");
if (rotateTheX) { objectToMove.Rotate(0, 0, Time.deltaTime * useNthSpeed);}
if (rotateTheY) { objectToMove.Rotate(0, Time.deltaTime * useNthSpeed, 0);}
if (rotateTheZ) { objectToMove.Rotate(Time.deltaTime * useNthSpeed, 0, 0);}
}
private void FollowWaypoints()
{
if(waypoints.Count < 2) { return; }
if(!moveAlongWaypoints) { return; }
if (Vector3.Distance(waypoints[currentWaypointIdx].position, transform.position) < distanceOfWaypointIsCloseEnough)
{
currentWaypointIdx++;
if (useRandomWaypoint)
{
currentWaypointIdx = Random.Range(0, waypoints.Count);
if(rndHit % numberForEveryNthTime == 0)
{
hitTargetCounter += 1;
rndHit = 0;
}
rndHit++;
}
if(currentWaypointIdx >= waypoints.Count)
{
currentWaypointIdx = 0;
hitTargetCounter += 1;
}
}
StartCoroutine(MoveItCoRountine(waypoints[currentWaypointIdx]));
}
IEnumerator MoveItCoRountine(Transform target)
{
yield return new WaitForSeconds(moveDelayWaypoints);
//Debug.Log(transform.gameObject.name + " is moving");
transform.position = Vector3.MoveTowards(transform.position, target.position, GetSpeedToMove(speedToMoveWaypoints));
}
private float GetSpeedToMove(float speedToMove)
{
//(useEveryNthTime && ((hitTargetCounter % numberForEveryNthTime == 0))) ? speedToMove * Time.deltaTime * multiplySpeedBy : speedToMove * Time.deltaTime;
if(useEveryNthTime && ((hitTargetCounter % numberForEveryNthTime == 0)))
return speedToMove * Time.deltaTime * multiplySpeedBy;
else return speedToMove * Time.deltaTime;
}
private void DropObjectsWithinYourRange()
{
if(!dropObject) { return; }
if(Time.time > (timeLastDropped + timeBetweenDrops))
{
//Drop it
var positionToDrop = new Vector3(Random.Range(minXvalue, maxXvalue), fromYvalue, Random.Range(minZvalue, maxZvalue));
try
{
numberOfObjectsDropped++;
//This makes the drops move with the parent object so the min's and max's change with object movement
//Destroy(Instantiate(myChildObjectPrefabToDrop, positionToDrop, Quaternion.identity, gameObject.transform), timeToDestroyAfterDrop);
//This makes the drops stay within the world min's and max's change with object movement
Destroy(Instantiate(myChildObjectPrefabToDrop, positionToDrop, Quaternion.identity), timeToDestroyAfterDrop);
}
catch (System.Exception)
{
Debug.Log("Again this exception thing");
}
timeLastDropped = Time.time;
}
}
}
I am working slowly on building a scene to demo this universal and versatile script.
Your ideas will help me a lot.
Thanks