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 Jogabba · Mar 18, 2014 at 07:05 PM · 2dmovementrandom

2D Random movement?

I'm trying to make a scene where a non playable character moves, for example, 3 units up, then stops for a sec, and then moves 3 units down, stops again, and then 3 units down, etc.

In other words, I'm trying to write a script that makes an object move a determinated distance with random direction (in the Y axis), stops for a moment, and moves again.

I'm pretty noobie programming in Unity, so any clues would be appreciated.

Oh, and I'm using a 2D environment, just in case.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Mar 18, 2014 at 09:26 PM

You might want to explore coroutines for this kind of repeating functionality with pauses. The following link explains coroutines. It uses C#, but the concepts are the same (and it is simpler to create and use co-routines in Javascript).

http://unitygems.com/coroutines/

Here is a short script that uses coroutines to implement your described movement:

 #pragma strict 
 
 var pauseTime : float = 1.0;
 var moveAmount = 3.0;
 var moveSpeed = 1.5;
 private var nextPos : Vector3;
 
 function Start() {
     MoveAndPause();
 }
 
 function MoveAndPause() {
     nextPos = transform.position;
 
     while (true) {
         nextPos.y += (Random.value < 0.5) ? moveAmount : -moveAmount;
         
         while (transform.position != nextPos) {
             transform.position = Vector3.MoveTowards(transform.position, nextPos, Time.deltaTime * moveSpeed);
             yield;
         }
     yield WaitForSeconds(pauseTime);
     }
 }



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 Jogabba · Mar 18, 2014 at 11:22 PM 0
Share

Thank you very much sir. You just made my day.

avatar image
0

Answer by Bovine · Mar 18, 2014 at 08:38 PM

If it is as simple as that, then you have a simple state machine and you would be either moving or waiting, the state would alternate between the two.

If it gets much more complex than this, which it probably will, then you can keep band-aiding states together, or look at some AI fundamentals and something like a behaviour tree, of which there are a couple on the asset store.

You can find more details over on: http://aigamedev.com/ Not everything on there is free, but there's some info, this one is free, but you do need to sign up:

https://aigamedev.com/insider/presentations/behavior-trees/

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d obstacle moving up and down question 1 Answer

Random Movement in 2D 2 Answers

How to create random movement in 2d 2 Answers

Random movement direction cycle. 2 Answers

Random Movement : 2d 1 Answer


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