Hey everyone, i´m making a ball rolling game and im trying to setup the start menu now.
What i want to happen is when i click my GUI button it will call a function which makes my camera move from its own position to a new position in a smooth transition.
The button will when its clicked call the Multiplayer function inside the script and its then ment to move the camera from its position to the “MultiTarget” position in as mentioned, a smooth transition.
But right now the only thing that happens is that it moves some inches towards the MultiTarget transform instead of all the way in a smooth transition, but i guess its the “Vector3.MoveTowards” that does it but i dont know what to replace it with to fix it tho ;3
#pragma strict
var MultiTarget : Transform;
var cam : Transform;
var Speed : float;
var Step : float;
function Start ()
{
cam = GameObject.Find("Camera").transform;
MultiTarget = GameObject.Find("Multi").transform;
}
function Update ()
{
Step = Speed * Time.deltaTime;
}
function Multiplayer ()
{
cam.transform.position = Vector3.MoveTowards(cam.transform.position, MultiTarget.position, Step);
}
I’m not sure where you’re calling Multiplayer from, but it’s probably only calling it for a single frame. If you want it to fully transition from Point A to Point B, then you need to make sure that it’s being called every frame until it’s in the new position. Vector3.MoveTowards() will take Point A and Point B, measure the distance, and move “step” amount towards the goal- that’s all, barely any movement in one frame, so it has to be run over and over and over.
One option is that you can run it from the Update function (multiplying by Time.deltaTime) or FixedUpdate (can ignore the time adjustment)- just flip a bool to true to cause it to transition each time Update is called and, once it’s at the goal, flip the bool off again so it stops. Another option is to run it as a coroutine instead- just put it in a loop and call “yield null” to wait for the next frame and adjust and loop again, until it reaches the goal position and ends itself (you can use a bool value here as well that you flip to false when it finishes, if you like).
Thanks for the help it all works now
I used the option with the variable and the movement being called in a FixedUpdate. Fun question is it possible to change a variable directly with a button or does it have to be etc “set to true” inside a called function?
You can directly change a public class member with a button’s OnClick() function (which you can see and set in the inspector without going into the code). Unfortunately, I don’t think you can set it to toggle though, just set it to absolute values like “on click, set to true”. If you call a function instead, then the function can have “boolValue = !boolValue”, which will just switch the value to the opposite.