Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by mark 1 · Apr 29, 2010 at 06:45 PM · 2dgameobjectmoving

left hand invaders moving into each other

here is the link http://gapti.com/GameTest/spaceinvader.html

and the enemy moving code.

var enemySpeed : int; var bullet : Rigidbody; static var moveDir = Vector3.right; enum AIState { Swoop, Shoot }

function Start () { state = AIState.Shoot; yield WaitForSeconds(Random.Range(5, 10)); PerformAction(state);

}

function Update () {

 var moveIt = enemySpeed * Time.deltaTime;

 ///make the enemy move side to side
 if (transform.position.x >= 6)
 {
     moveDir =   Vector3.left;
 }

if (transform.position.x <= -6.0) { moveDir = Vector3.right; } transform.Translate(moveDir * moveIt);

}

function PerformAction( currentState : AIState ) {

 switch (currentState) {
     case AIState.Swoop: 
         print("swooping");
         break;

      case AIState.Shoot:
         print("shoot");
         var tempBullet: Rigidbody;
         tempBullet = Instantiate(bullet, transform.position, transform.rotation);

         Start();
         break;

     // etc...
 }

}

Comment
Add comment · Show 1
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 straydogstrut · Apr 29, 2010 at 07:21 PM 0
Share

$$anonymous$$ight want to tidy up your code a bit, and explain what your question is (Edit your original question). I don't see anything wrong with your game myself.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by duck · Apr 29, 2010 at 07:47 PM

Each frame, your aliens each execute their own Update, one at a time.

What is probably happening, is that when left hand invader hits the edge, it's the last of the group to perform their Updates, so all the others have already moved left a bit further, and now this onem when taking its turn, moves right as a result of the change of direction - on the same frame that the others moved left.

To fix this, you could delay changing direction until every enemy has moved using LateUpdate, like this (irrelevant code removed):

static var moveDir = Vector3.right; static var newMoveDir = Vector3.right;

function Update () {

 var moveIt = enemySpeed * Time.deltaTime;

 ///make the enemy move side to side
 if (transform.position.x &gt;= 6)
 {
     newMoveDir =   Vector3.left;
 }
 if (transform.position.x &lt;= -6.0)
 {
     newMoveDir =   Vector3.right;
 }
 transform.Translate(moveDir * moveIt);  

}

function LateUpdate() { moveDir = newMoveDir; }

So, every alien uses 'moveDir', even if one of the group has indicated that it's time for a change of direction. And then after every Update has completed, LateUpdate gets called, which assigns the new move direction.

Comment
Add comment · Show 2 · 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 mark 1 · May 15, 2010 at 04:54 PM 0
Share

Thanks that worked

avatar image duck ♦♦ · May 15, 2010 at 09:21 PM 0
Share

no problem - remember to mark the question as 'accepted' by clicking the tick mark.

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

No one has followed this question yet.

Related Questions

how to tell a game object to move between 2 points(A,B) along x axis , right to left, and then move left to right back to its first position ??? 3 Answers

How to move a GameObject with an animation 2 Answers

Scripts wont work if deactivated then activated? 0 Answers

How to use the scene edit for Line Renderer? 1 Answer

How to scroll progressively a game object? 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