I am working on GUI textures to move my player using a simple script which i have made to do this …
when I am moving my player I have to clicked the GUI every time. what i want is the player should move continuously every frame when I clicked the GUI button once by holding it till my player reaches a particular point…
here is the code:
var Player : GameObject;
var speed =8.0;
function OnGUI () {
if(GUI.Button(Rect(200,250,80,20),"Left"))
{
Player.transform.Translate(-speed * Time.deltaTime,0,0);
}
if(GUI.Button(Rect(280,250,80,20),"Right"))
{
Player.transform.Translate(speed * Time.deltaTime,0,0);
}
if(GUI.Button(Rect(240,230,80,20),"Up"))
{
Player.transform.Translate(0,0,speed * Time.deltaTime);
}
}
with this code, what is happening with me is i have to click it every time to move a single step. what should i do in this code to move the player continuously …
And how should i convert the local space of my player to world space…?
the answer to the first part is use a repeat button.
I am guessing for the second part you are saying you want the camera to follow the box, either make the camera a child so it follows or use the smooth follow script.
for world space use this
transform.Translate(0,0,speed * Time.deltaTime, Space.World);
or if you just want to know what it is you’ll find the value in transform.position
thanks TheRaider: the GUI.RepeatButton works for me now the player is moving continuously till i hold the button
But shaderx Space.World is not showing too much difference in the game play…
You definitely do not want to modifying a transform in OnGUI(). OnGUI() is called multiple times per frame, and should not be used for anything but rendering GUI controls. You should instead make the gui buttons flip boolean functions, and then perform the translations in Update() based on those booleans.
@legend411 thanks for tip, but can you show me an example i don’t know the method that you told me to do. please… its seems interesting
legend, couldn’t you get around the problem by just multiplying by deltatime?