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 siddharth3322 · Jan 15, 2014 at 09:42 AM · unity 2dmove an objectunity4.3

Random Object movement

I am trying to create Bird Hunting type game in 2d environment using Unity 4.3. For which I have to move my bird here and there so that it become difficult to shoot the bird.

For this I want help from you guys. What is the easiest way to achieve this? If you want any more help then I am ready to provide. I think my question is pretty basic but I am new to this engine.

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 siddharth3322 · Jan 16, 2014 at 05:43 AM 0
Share

One method available is $$anonymous$$athf.PingPong. Is that help me in current situation?

2 Replies

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

Answer by gfoot · Jan 15, 2014 at 10:05 AM

One simple way to do this is to make the bird wait a random amount of time and then pick a nearby location to move to; then move there, and loop.

You can do this quite clearly using coroutines, something like this:

 public void Start()
 {
     StartCoroutine(BirdBrain());
 }

 public IEnumerator BirdBrain()
 {
     while (true)
     {
         yield new WaitForSeconds(Random.value * MaxWaitTime);

         Vector3 localTarget;
         localTarget.x = (Random.value * 2 - 1) * RandomPositionXRange;
         localTarget.y = (Random.value * 2 - 1) * RandomPositionYRange;
         localTarget.z = (Random.value * 2 - 1) * RandomPositionZRange;

         Vector3 targetPosition = transform.position + localTarget;

         while (transform.position != targetPosition)
         {
             yield return null;
             transform.position = Vector3.MoveTowards(transform.position, targetPosition, MoveSpeed * Time.deltaTime);
         }
     }
 }

That's just off the top of my head but hopefully illustrates one way to do this, and how obvious the flow can be when you're using coroutines. The most important thing to remember with coroutines is to make sure that every loop either contains a yield statement, or is sure to exit promptly.

Comment
Add comment · Show 3 · 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 siddharth3322 · Jan 16, 2014 at 05:40 AM 0
Share

Thanks for your reply gfoot. I want to understand these line of code from you.

localTarget.x = (Random.value 2 - 1) RandomPositionXRange; localTarget.y = (Random.value 2 - 1) RandomPositionYRange; localTarget.z = (Random.value 2 - 1) RandomPositionZRange;

avatar image gfoot · Jan 16, 2014 at 09:11 AM 0
Share

Random.value is from 0 to 1; those expressions just covert it to be from -1 to 1 and then multiply by some scale value.

You can ins$$anonymous$$d write Random.Range(-RandomPositionXRange, RandomPositionXRange) and get the same effect.

avatar image siddharth3322 · Jan 16, 2014 at 10:24 AM 0
Share

Thanks for simple explanation. Using this code I got my question's answer.

avatar image
0

Answer by siddharth3322 · Jan 16, 2014 at 10:46 AM

This link also become helpful in creating random object movement because in that person also specify the bounds within which object moves.

Random Movement in 2D

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

20 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

Related Questions

Creating Menu in 2D Game 0 Answers

Unity 2D: move towards mouse and through mouse 1 Answer

How to make paint coloring game in unity and COLOR specific part of image 0 Answers

How to use crossplatforminput buttons as getaxisraw("Horizontal") 1 Answer

I cant use MoveGameObjectToScene 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