Move 3D object on transform.localposition
----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
}
}
}
Answer by tonyjwhite · Oct 25, 2018 at 11:25 AM
I was able to generally fix the problem I was having by added the script onto the parent itself, instead of each enemy--its not specifically what I wanted, but it will work for what I am doing. I did however have to add a new public variable, public float MaxLow = -2f;
. and change Line 21 float newposistionMin = MaxHigh - initialPos.y;
to float newposistionMin = MaxLow + initialPos.y;
--all the enemies now move exactly how they should, but now theyre all moving at the same time( I was hoping to keep them individual.)
---If anyone can still give me any ideas on how to make it work per object, id really like to know.
Your answer
Follow this Question
Related Questions
Trying to transform my character's local position on a GetKey. 0 Answers
Reversing movement directions 1 Answer
Move a object along a vector 1 Answer
GameObject doesn't move 1 Answer