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 17, 2014 at 12:25 PM · animationunity4.3moving-animations

Random Entry of Object

At present I am developing Bird Hunting game in 2D. For which I want to create animation like bird come into screen from random position. I know about creating animation at design time but at present I want to create run time animation.

So how to achieve this? Small suggestion become enough for me.

Comment
Add comment · Show 2
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 aeroson · Jan 17, 2014 at 12:34 PM 0
Share

Do you mean a smooth transition of bird's position from point A to point B ?

avatar image siddharth3322 · Jan 17, 2014 at 01:01 PM 0
Share

Yes, you understand this right but both position is random for bird. There is no rigid body attached to game object.

1 Reply

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

Answer by aeroson · Jan 17, 2014 at 02:22 PM

The fastes way to do this, is to instantiate bird at random position and set his endPosition.

 GameObject instantiatedBird=GameObject.Instantiate(birdPrefab,Random.insideUnitSphere,Quaternion.identity);
 instantiatedBird.GetComponent<BirdLogic>().endPosition=Random.insideUnitSphere;


Then in BirdLogic you would do this

 public Vector3 endPosition; // bird's target position
 Vector3 startPosition;
 float time=0; // 

 void Start() {
     startPosition = transform.position; // remember start position
 }
 void Update() {
     time+=Time.deltaTime;
     time=Mathf.Clamp01(time); // make sure time is between 0 and 1
     transform.position = Vector3.Lerp(startPosition, endPosition, time);
 }


If you wish to set the bird's rotation according to the direction he moves, add this to the Update function:

 transform.rotation = Quaternion.LookRotation(endPosition-transform.position);
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 18, 2014 at 05:35 AM 0
Share

Thanks for you great help. If I want to choose location from outside the camera visible region then how to get that location.

avatar image aeroson · Jan 18, 2014 at 08:59 AM 0
Share

http://answers.unity3d.com/questions/405707/spawn-outside-camera-view.html

Camera.ViewportToWorldPoint (x=0 is left, x=1 is right, y=0 is bottom, y=1 is top)

Random.value returns random number from 0 to 1.

This will make start pos left of camera from 0.5 to 1.5 times the width of camera. Note the width of camera depends on how far from camera the point is, change to 10.0f to adjust that.

 Vector3 startPos = Camera.main.ViewportToWorldPoint(new Vector3(-0.5f-Random.value,Random.value,10.0f));
 






$$anonymous$$ake end pos within camera view, that is from 0,0 to 1,1

 Vector3 endPos = Camera.main.ViewportToWorldPoint(new Vector3(Random.value,Random.value,10.0f));


avatar image siddharth3322 · Jan 18, 2014 at 09:05 AM 1
Share

Thank you so much for you help and now I mark your answer as correct because it already work for me.

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

19 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

Related Questions

Single Time Animation Play 1 Answer

Crushy Bird Game Doubt 1 Answer

Detect animation completion in iTween 2 Answers

Move Object Using iTween 2 Answers

2D game animation is running 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