- Home /
Trying to stop object snapping to a position
Hi i have got a problem that i can't work out how to fix without break my code. i was wondering if anyone could possibly help change my code below.
What this code does, is that tells the game objects to go to their end points, which are empty game object that act as reference points in virtual space, by a click of a GUI button.
At the moment the code works but it snaps the object to their end point instead of flowing (so you get to see the movement from A to B)
What i want to achieve is this "flow movement" and i have been told in the past that this can be done with delta.time or something on the lines of that.
I would be grateful for any help thank you.
var InnerCog : GameObject; var InnerRing : GameObject; var LeftScrew : GameObject; var OuterCog : GameObject; var OuterNut : GameObject; var OuterRing : GameObject; var RightScrew : GameObject; var Shaft : GameObject; var InnerShaftRing : GameObject; var InnerTopRing : GameObject; var SecondShaftRing : GameObject; var Top : GameObject;
var ICEnd : Transform; var IREnd : Transform; var LSEnd : Transform; var OCEnd : Transform; var ONEnd : Transform; var OREnd : Transform; var RSEnd : Transform; var SEnd : Transform; var ISREnd : Transform; var ITREnd : Transform; var SSREnd : Transform; var TEnd : Transform;
function OnGUI () { if (GUI.Button (Rect (760,10,90,25), "Explode")) { InnerCog.transform.position = ICEnd.transform.position; InnerRing.transform.position = IREnd.transform.position; LeftScrew.transform.position = LSEnd.transform.position; OuterCog.transform.position = OCEnd.transform.position; OuterNut.transform.position = ONEnd.transform.position; OuterRing.transform.position = OREnd.transform.position; RightScrew.transform.position = RSEnd.transform.position; Shaft.transform.position = SEnd.transform.position; InnerShaftRing.transform.position = ISREnd.transform.position; InnerTopRing.transform.position = ITREnd.transform.position; SecondShaftRing.transform.position = SSREnd.transform.position; Top.transform.position = TEnd.transform.position; } }
//Written by Will Poole "Explode Script" 6/12/10
Answer by Statement · Dec 07, 2010 at 04:06 PM
Try this code. You need to call StartCoroutine with Move for each pair of object you got.
function OnGUI () { if (GUI.Button (Rect (760,10,90,25), "Explode")) { var speed = 1.0f; StartCoroutine( Move( InnerCog, ICEnd, speed ) ); StartCoroutine( Move( InnerRing, IREnd, speed ) ); // And so on... } }
function Move(GameObject from, Transform to, float speed) { while (from.transform.position != to.position && speed > 0) { from.transform.position = Vector3.MoveTowards(from.transform.position, to.position, speed * Time.deltaTime); yield; } }
Answer by skovacs1 · Dec 07, 2010 at 03:18 PM
Assigning the position like this doesn't even show the movement from A to B. Assigning the position simply jumps to B. Flowing? I'm not sure exactly what you mean by this as we aren't talking about a fluid simulation or anything, but I'll assume that you mean moving or moving with easing.
A few notes:
- No need to store the GameObject if you don't use it.
- You don't need to get the transform when you're already storing it.
- You shouldn't do your movement in OnGUI as that is called multiple times per frame. Put movement in an Update function if you can.
To move to the position from A to B, you would probably want to use Lerp. To get constant movement, you would store the start position as well as the end. Something like:
var subject : Transform; var start : Transform; var end : Transform; var speed : float = 0.5f; private var control : float = 1.0f;;
function OnGUI() { if(control >= 1.0f && GUI.Button(Rect(760,10,90,25),"Move") control = 0.0f; }
function Update() { if(control < 1.0f) { control += Time.deltaTime speed; subject.position = Vector3.Lerp(start.position,end.position,control); ///If you want to add easing subject.position = Vector3.Lerp(start.position,end.position, Mathf.SmoothStep(0.0f,1.0f,control));/ ///Swap start and end to go back and forth //There are other ways to do it, but they involve clamping, etc. if(control >= 1.0f) { var temp : Transform = start; start = end; end = temp; }*/ } }
If you want to store the start without having to have another transform for it, you could do something like this:
var subject : Transform; var endPoint : Transform; var speed : float = 0.5f; private var control : float = 1.0f;; private start : Vector3 = Vector3.zero; private end : Vector3 = Vector3.zero;
function Start() { start = subject.position; if(endPoint) end = endPoint.position; }
function OnGUI() { if(control >= 1.0f && GUI.Button(Rect(760,10,90,25),"Move") control = 0.0f; }
function Update() { if(control < 1.0f) { control += Time.deltaTime * speed; subject.position = Vector3.Lerp(start,end,control); } }
To ease in only towards the end, you can Lerp from subject.position to the end.position, but then your movement is framerate dependent. When doing this, you typically want to lerp by some fixed amount as if you were to lerp with Time.deltaTime, when your frame takes a longer amount of time, your movement would move by larger amounts and for a frame that took 1 second, your subject would jump to the end.position. The other concern is that unless you use Time.deltaTime and a frame takes 1 second, your subject will never reach end exactly, but will be approaching it forever so you cannot compare the two positions for equality (which is generally not a safe idea anyways). To aleviate framerate dependencies, you can place the movement in FixedUpdate.
This setup doesn't seem very scalable. If you add or remove objects, you need to change the script. You might be better off having a control script and a movement script on the objects you want to move:
MoveButton.js (put this on the camera or something)
function OnGUI() {
if(!Mover.moving && GUI.Button(Rect(760,10,90,25),"Move")
Mover.moving = true;
}
Mover.js (put this on each object you want to move)
static var moving : boolean = false; var endPoint : Transform; var speed : float = 0.5f; private var control : float = 1.0f;; private start : Vector3 = Vector3.zero; private end : Vector3 = Vector3.zero;
function Start() { start = subject.position; if(endPoint) end = endPoint.position; }
function Update() { if(control < 1.0f) { control += Time.deltaTime * speed; subject.position = Vector3.Lerp(start,end,control); if(control >= 1.0f) { control = 0.0f; moving = false; } } }