- Home /
Scripting a GUI button to move object smoothly
Hi Im having trouble with the Js ive written below.
Im creating a GUI button that when clicked moves the player to a defined position using the SmoothDamp function. But cant seem to get the code working and help or tips would be excellent.
var target : Transform; var smoothTime = 0.3; private var velocity = Vector3.zero;
function OnGUI () { if (GUI.Button (Rect (150,10,140,70), "Fly me to earth")) {
OnMouseDown () {
var targetPosition : Vector3 = target.TransformPoint(Vector3(0, 5, -30));
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, velocity, smoothTime);
}
}
}
Thanks in advance J
ok Ive got it to work by removing the
On$$anonymous$$ouseDown () {
but it only moves a short distance every time you click the GUI button.
How do I make it carry out the whole transform rather than bit when button is pressed?
Answer by doublethink · Jan 17, 2013 at 10:55 PM
Try creating a MoveTo(destination) method that moves the player in increments towards the destination and keeps moving until it reaches it. You can then re-use the method to move the player wherever you want and at whatever speed you want.
Hey thanks Doublethink
Would I still be able to set a target as in initial try?
Also a little help in how the script would look would be really useful as I'm pretty new to javascript and just finding my feet.
Thanks