- Home /
The question is answered, right answer was accepted
Gameobject not moving unless starting at 0 transform
I'm making a 2d game where the enemy is assigned a array of vector3's in the editor. That way I can set it's path along a grid of units that it moves to in a blocky way. It was working well but, after I changed the orgin point of the enemy gameobject off of 1,0,0 it doesn't work. I couldn't find any noticeable changes in any values while looking in the debug menu and I attempted to use some debug logs but they also didn't reveal anything. Note: I am using unity 4 as that's all my laptop supports.
Code:
using UnityEngine;
using System.Collections;
public class EnemyMovementController : MonoBehaviour {
//Set in editor by level basis, holds pathfinding data for enemy.
public Vector3[] EnemyPathArray;
//Path segment in the Enemy Path Array.
int PathSegmentArray;
//Speed for movement;
public float EnemyMovementSpeed;
//Timer on when the enemy moves.
float Speed;
float EnemyMoveTimer;
//Temp float for testing the time between moves for ideal timeing.
public float EnemyMoveTimerCapTemp;
//Bool for moving enemy
bool EnemyMoving;
//Movement Array for enemy position.
public GameObject Enemy;
void Start(){
EnemyMoveTimer = EnemyMoveTimerCapTemp;
PathSegmentArray = 0;
}
void Update(){
if (!EnemyMoving) {
EnemyMoveTimer -= Time.deltaTime;
if (EnemyMoveTimer <= 0) {
EnemyMoving = true;
}
}
if (EnemyMoving) {
Speed = EnemyMovementSpeed * Time.deltaTime;
MoveEnemy();
}
}
void MoveEnemy(){
//Debug.Log ("Moving Enemy");
Debug.Log (PathSegmentArray);
Enemy.transform.position = Vector3.MoveTowards (Enemy.transform.position, EnemyPathArray [PathSegmentArray], Speed);
//Debug.Log (test + "this.transform");
//Debug.Log (EnemyPathArray [PathSegmentArray].y + "Target transform");
if (Enemy.transform.position == EnemyPathArray [PathSegmentArray]) {
PathSegmentArray += 1;
if(PathSegmentArray > EnemyPathArray.Length - 1){
PathSegmentArray = 0;
//Debug.Log("Setting path segment to 0");
}
EnemyMoveTimer = EnemyMoveTimerCapTemp;
EnemyMoving = false;
}
}
}
note: I debug logged the position vector of the enemy right after the movetowards func should be getting called. It says that stuck at -.9 on the x. I'm trying to go from -1x to 0 x and it's getting stuck at -.9.
O$$anonymous$$! It's the animator that's causing it. Idk what is wrong with it but I'm gonna fix it.
Yep my Enemy's idle animation was screwing it up. It didn't actually have anything in it and i think it was causing problems one way or another.