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 zero_null · Mar 27, 2014 at 09:19 AM · instantiate

Problem translating game objects

I am instantiating my gameobjects (spheres) through script. Now I want them to move from one place to some other every frame. I want my spheres move from one location to other. but I don't know how to do this. Update is called once per frame. Is this worth doing? to do this all in an update function? or I know I can't use OnGUI(), as it is just a liability. I am using unity free. I don't want to use special plugins. My budget didn't allows me.

Comment
Add comment · Show 3
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 Destran · Mar 27, 2014 at 10:00 AM 0
Share

If you're wanting something to move every other frame I'd look into yield. (Can't use it in Update, so you'd have to call use a coroutine)

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Coroutines_26_Yield.html

But you should know that if you're running at 60fps, that sphere will be moving/teleporting extremely quickly (around 30 locations a second).

Overall, your code might look something like this:

 #pragma strict
 
 
 var target : Vector3;
 var moving : boolean; //you don't want to endlessly start coroutines, so give it a boolean
 
 function Start() {
 
 }
  
 function Update() 
 {
     target = Random.insideUnitSphere * 3;// 3 is the RADIUS of the sphere, you can change
 if(!moving)
     {
     Randomly$$anonymous$$oveSphere(); //calls the coroutine
     }
 
 }
 function Randomly$$anonymous$$oveSphere()
 {
 moving = true;
 transform.position = target;
 yield WaitForSeconds(1);  // you can replace this line with just " yield; " for it to happpen every other frame
 moving = false;
 }
avatar image PorkMuncher · Mar 27, 2014 at 10:32 AM 0
Share

If I understood right you need to move something over time.

Lerp is an easy way to move something smoothly over period of time!

Official Unity tutorial on lerps: https://www.youtube.com/watch?v=cD-mXwSCvWc

avatar image Destran · Mar 27, 2014 at 10:38 AM 0
Share

Hmmm, that probably is what he meant. whoops. I read " from one place to some other every frame" as " from one place to some other every other frame".

1 Reply

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

Answer by NoseKills · Mar 27, 2014 at 12:49 PM

The Update method is the right place to move objects. Don't worry about performance before you have some hundred GameObjects moving, since it's usually rendering that will kill the performance anyways, not simple scripts like this. Update is called as often as the game renders, whereas OnGUI for example gets called more often and you'd be moving the object more than once and then drawing it on the screen where it was left after the last move, which doesn't make sense.

You could add a script like this to the objects you need to move. If you want them to actually move every frame, delete all code except "moveToRandomSpot()" from inside update.

 using UnityEngine;
 
 public class Mover : MonoBehaviour
 {
     const float DELAY_MAX = 100f; // in seconds
     const float DELAY_MIN = 10f;
 
     const float POSITION_MIN = -100f; // in world units
     const float POSITION_MAX = 100f;
 
     private float _MoveDelay;
 
     void Start()
     {
         setDelay();
     }
 
     void Update()
     {
         _MoveDelay -= Time.deltaTime;
         if (_MoveDelay < 0)
         {
             moveToRandomSpot();
             setDelay();
         }
     }
 
     private void setDelay()
     {
         _MoveDelay = Random.Range(DELAY_MIN, DELAY_MAX);
     }
 
     private void moveToRandomSpot()
     {
         transform.position = new Vector3(Random.Range(POSITION_MIN, POSITION_MAX), 
                                          Random.Range(POSITION_MIN, POSITION_MAX), 
                                          Random.Range(POSITION_MIN, POSITION_MAX));
     }
 }
 
Comment
Add comment · Show 4 · 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 zero_null · Mar 27, 2014 at 01:15 PM 0
Share

Nose$$anonymous$$ills Can I ask for more help? :( Please? Dude I am making a snake game for android. I made it through GUITexture and it works pretty fine. But now I had to add more features in it. I have created all the snake, food, piece on the fly through onGUI() :( , but now it is beco$$anonymous$$g cumbersome. As I can't make 10 stages(levels of game) through c# script. That would be a very huge overhead on the OnGUI() method to check if user had a particular score & promote him to next stage (which will also be made dynamically).

avatar image zero_null · Mar 27, 2014 at 01:19 PM 0
Share

Nose$$anonymous$$ills: If you can help me out can you please tell me. how I can rotate my snake head at a particular 90 degree to left when user swipes left, when he swipes right it should move right, same for up and down swiping? how can I achieve this? $$anonymous$$y snake rotates 90 degrees.

avatar image zero_null · Mar 27, 2014 at 01:21 PM 0
Share

Look at in this tutorial

I am making game similar to this game.

avatar image zero_null · Mar 27, 2014 at 01:23 PM 0
Share

I was following this one earlier. I made this for android and it's working fine. I am a beginner and have no prior experience in unity . :(

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

Instantiate problem. 3 Answers

How can I instantiate more prefabs as the score increases? 2 Answers

Clone Prefab Duplicate Problem 1 Answer

After launching game on Windows x32 the script working with MySQL database could not be instantiated 0 Answers

Cloud recognition in Vuforia 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