- Home /
One Enemy Object Moves, But The Others Do Not
I have a scene with three enemies in it. They all share the same script. I just duplicated the first enemy gameObject. The original enemy gameObject works exactly as I want it to, but the other two don't.
Here is my code:
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour {
//Variables
public float speed = 2.0f;
private float boundary1;
private float boundary2;
private bool goingLeft;
public int left;
public int right;
// Use this for initialization
void Start () {
boundary1 = transform.position.x - left; //left boundary
boundary2 = transform.position.y + right; //right boundary
goingLeft = false;
}
// Update is called once per frame
void Update () {
if (transform.position.x < boundary2) { //less than right boundary going right
if (goingLeft == false)
transform.Translate (Vector3.right * Time.deltaTime * speed);
}
else
goingLeft = true;
if (transform.position.x > boundary1) { //greater than left boundary going left
if (goingLeft)
transform.Translate (Vector3.left * Time.deltaTime * speed);
}
else
goingLeft = false;
}
}
Basically what my object does, is go continuously between one boundary and the other. Works for one gameObject, but the two other ones just go to one boundary and stops. I don't really understand how that could happen. What is going wrong?
It works perfectly for me. click on gameobject and in the inspector in the top right corner there are 3 lines near the lock. click it and click Debug. then in the inspector you can see the variables and what happening to them while play.
How did you get it to work? Is there some sort of setting that i'm missing? I put it on debug, but the variables all seem to be fine. The boundary variables are right, and when the enemy reaches the left boundary the GoingLeft variable changes to false. I'm not sure what I'm missing.
I just put a Cube in the scene, gave your script then duplicated 2 times. all the 3 cubes were moving left to right. I tried to change the left and right values and rotating the object. Still worked perfectly. I recommend you to do the same, then always change a little bit (for example change the gameobject from cube to your character) to look like your scene and you will find out what is the problem.
Ok, I created a new scene and put just one cube and attached the script but it's still only going to the left and then stopping. I tried adding a rigidBody and a floor cube to see if that would affect anything but still the same result. This is really confusing me! I looked at what the variables are doing, and it looks like it goes to the left boundary, changes the variable goingLeft to false and then it appears to stop. Any ideas on what I should do?
Answer by Digital-Phantom · Apr 04, 2015 at 02:02 PM
try making
private bool goingLeft;
into
static bool goingLeft;
:)
That didn't fix it. The other two still don't move, and the first one goes left, then right, then stops.
Answer by lordlycastle · Apr 04, 2015 at 10:15 PM
Player is always going to be between left and right boundary, so both if statements are always going to be true. Kinda beats it’s purpose. So the else part will never the used. Below I check when player moves out of the boundary, when he does, I change the direction. Try this:
if (transform.position.x > boundary2 || transform.position.x < boundary1)
goingLeft = !goingLeft;
var dir = goingLeft ? Vector3.left : Vector3.right;
transform.Translate (dir * Time.deltaTime * speed);
Well, in my if statements I also have a variable GoingLeft, and I change that to true or false to make sure that it is going in the direction that I want it to, so the else statements are actually being used.
I'll try changing it for that code though. Do you know how I could change that into c# because I don't know javascript
@ukcharlie The code above is C#. And yes you are correct, it does go in the else statements, got confused, it’s not easy to follow. The code above is practically the same except the conditions are inverted, so makes it more readable I guess.
oh whoops. I saw var and though it was javascript! I put in that code but it didn't work right.
@ukcharlie45 It would help if you tell what didn’t work right. Remember you have to set initial value of goingLeft.
Answer by hbalint1 · Apr 11, 2015 at 10:18 PM
Maybe I found the answer. You should only use two positive numbers as the two boundaries. If you put negatives (or one negative, one poisitive) it will give strange result and it will stop. If i try with two positives then it's erfect, but if i try like: right= -5; left = 3; I get the same result as you. So only use positive numbers!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity5 filled my SSD? 0 Answers
How to move rigidbody a certain distance, rather than constantly 1 Answer
Generate floor mesh according to walls 0 Answers