- Home /
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.
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;
}
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
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".
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));
}
}
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).
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.
I am making game similar to this game.
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 . :(