Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ukcharlie45 · Apr 04, 2015 at 01:07 PM · c#unity 5

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hbalint1 · Apr 04, 2015 at 01:21 PM 0
Share

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.

avatar image ukcharlie45 · Apr 04, 2015 at 09:48 PM 0
Share

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.

avatar image hbalint1 · Apr 05, 2015 at 08:18 AM 0
Share

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.

avatar image ukcharlie45 · Apr 11, 2015 at 09:44 PM 0
Share

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?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Digital-Phantom · Apr 04, 2015 at 02:02 PM

try making

private bool goingLeft;

into

static bool goingLeft;

:)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ukcharlie45 · Apr 04, 2015 at 09:39 PM 0
Share

That didn't fix it. The other two still don't move, and the first one goes left, then right, then stops.

avatar image
0

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);
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ukcharlie45 · Apr 04, 2015 at 10:57 PM 0
Share

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

avatar image lordlycastle · Apr 04, 2015 at 11:21 PM 0
Share

@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.

avatar image ukcharlie45 · Apr 11, 2015 at 09:50 PM 0
Share

oh whoops. I saw var and though it was javascript! I put in that code but it didn't work right.

avatar image lordlycastle · Apr 16, 2015 at 03:41 AM 0
Share

@ukcharlie45 It would help if you tell what didn’t work right. Remember you have to set initial value of goingLeft.

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges