• 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 ferpa · Oct 24, 2014 at 05:02 AM · c#gameobjectpositionrandommove

Moving GameObject to various position ?

Hello

I have a gameobject that I want to move to various position in scene like a pinball. The GameObject is a balloon and I want to move left, right, top and down randomly in scene.

How can I do this ?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Itaros · Oct 28, 2014 at 03:05 AM

Obviously, start by creating a script. You can change position of an object by performing:

 private Vector3 moveVector;
 void Update(){
  moveVector = Vector3.up;
  transform.Translate(moveVector*Time.deltaTime);
 }

where by "moveVector = Vector3.up" you set where to move.

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 ferpa · Oct 28, 2014 at 03:16 AM 0
Share

yep, but how can I do use Random position ?

avatar image Itaros · Oct 28, 2014 at 03:25 AM 1
Share
 private float refreshCooldown;
 private const float maxCooldown=1F;
 private Vector3 moveVector;
 void Start(){
 moveVector=Vector3.zero;
 }
 void Update(){
 refreshCooldown-=Time.deltaTime;
 if(refreshCooldown<0F){
 moveVector.x = Random.Range(-1F,1F);
 moveVector.y = Random.Range(-1F,1F);
 moveVector.z = Random.Range(-1F,1F);
 refreshCooldown=maxCooldown;
 }
 transform.Translate(moveVector*Time.deltaTime);
 }

That way it will go in absolutely random direction with changing it each second.

Btw, you can use Random.insideUnitSphere too but I have no idea how hard it is on allocations.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Solved] How to get non repeating random positions? 5 Answers

Select a random GameObject (Rain) (C#) 1 Answer

I tried to make a box to select units like you do in windows 1 Answer

iTween MoveTo shoots off into distance 0 Answers

Simultaneously moving GameObjects using C# script. 3 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