- Home /
Tween to moving target
Hi Guys,
Im trying to tween a GameObject from one position and orientation to another, the problem is the target destination is another object that is constantly shifting its position and orientation. After trying iTween unsuccessfully I wrote this code:
// percentage is a value between 0-1, for how far between the two object is
Vector3 DummyPosition = OriginDummy.transform.position;
Vector3 TargetPosition = TargetPosition.transform.position;
Phone.transform.position = (DummyPosition * (1.0f - percentage)) + ( TargetPosition * percentage);
Vector3 DummyForward = OriginDummy.transform.forward;
Vector3 TargetForward = TargetPosition.transform.forward;
Phone.transform.forward = ((DummyForward * (1.0f - percentage)) + ( TargetForward * percentage)).normalized;
Vector3 DummyRight = OriginDummy.transform.right;
Vector3 TargetRight = TargetPosition.transform.right;
Phone.transform.right = ((DummyRight * (1.0f - percentage)) + ( TargetRight * percentage)).normalized;
Vector3 DummyUp = OriginDummy.transform.up;
Vector3 TargetUp = TargetPosition.transform.up;
Phone.transform.up = ((DummyUp * (1.0f - percentage)) + ( TargetUp * percentage)).normalized;
which almost worked but one of the axis is off, I think the problem is that the forward,up etc. values are local, where as I need global axis. But I could be wrong, but surely there's a much better way to do this?
Thanks for any help
Answer by ThermalFusion · Oct 05, 2012 at 11:34 PM
Phone.transform.position = Vector3.Lerp(OriginDummy.transform.position, TargetPosition.transform.position, precentage);
Phone.transform.rotation = Quaternion.Slerp(OriginDummy.transform.rotation, TargetPosition.transform.rotation, precentage);
Good solution for FixedUpdate() -- Anyone know how to write the exact same functionality for Update()?
$$anonymous$$ultiply the percentage by Time.deltaTime if you want it to be in the Update().
There is nothing here that is FixedUpdate() specific. The thing that may change between FixedUpdate() and Update() would be how 'percentage' is calculated frame-to-frame, but the code to increment/change 'percentage' is not included either in the original question or in this answer.
Your answer