----Hello there!, I am currently trying to move this object back and forth at set speeds on the Y Axis. I have saved this script for my Enemy Controller, and have it added to several “enemy” 3D objects. Unfortantly the objects do not seem to be moving based on their own local position, but instead are all following the same local position(which I cannot seem to figure out where or what’s controlling it). I have them separated using different parents and moving those around, but am still unable to figure out whats wrong.
----The Enemy objects placed at the bottom of my environment are moving far upwards than they should, but they move back and forth as expected.(they go up 5, then bounce -4, +4, -4, +4 FROM their current position.{bounce between 3 and 8 on the Y Axis})
----The Enemy objects placed at a higher elevation in my environment are moving downwards, than stopping, and infinintly vibrating in place.
this is my third time going through the code to see if I could fix it myself, but im still learning, and im clearly having issues with something that feels very simple. Heres a Sample of my Current Code.
using UnityEngine;
public class MyEnemyController : MonoBehaviour {
public float speed = 3f; //Speed at which the objects move in Time.deltaTime
public float speedInc = 1.2f; //20% increase in speed for objects moving downward
public float maxHigh = 2f; //Maximum movement from current position
public float direction = 1f; //Rate at which to move
Vector3 InitialPos; //variable for 3D object X,Y,Z
void Start() {
//Saving Current Objects coordinates(X,Y,Z) into variable "InitialPos"
InitialPos = transform.localPosition; }
void Update () {
//Saving Maximum Range for objects movement into 2 different variables- one for the Maximum, the other for the Minimum
float newPositionMax = maxHigh + InitialPos.y;
float newPositionMin = maxHigh - InitialPos.y;
//If the object is moving in a downward position (were only moving on the Y Axis)
if(direction == -1){
//Saving base movement * 20% increase speed & directional movement into a variable "movementYDown"
float movementYDown = speed * speedInc * Time.deltaTime * direction;
//Forcing object to move on the Y Axis using Transform.up, and to use the speed and direction input from "movementYDown" variable
transform.position += transform.up * movementYDown;
}
else //Otherwise If the object is moving in any other direction thats not downward
{
//Saving base movement & directional movement into variable "movementY"
float movementY = speed * Time.deltaTime * direction;
//Forcing object to move on the Y Axis using transform.up and to use the speed and direction input from "movementY" variable
transform.position += transform.up * movementY; }
//If the Objects current position on Y Axis is "Greater-than" or "Equal-too" the Maximum Heighth we have set
if(transform.position.y >= newPositionMax) {
direction = -1; //Then move downward in a negative direction
}
//Else If the objects current position on Y Axis is "Less-than" or "Equal-too" the Minimum Heighth we have set
else if(transform.position.y <= newPositionMin) {
direction = 1; //Then move Upward in a positive direction
}
}
}