Each of these things works fine by itself but I can’t seem to correctly combine them to make them work together. The first 3 lines move an object in an orbit around another object. The fourth line moves the object forward and backward on the z axis. How would I make both of these things happen at the same time?
transform.RotateAround (transform.parent.position, axis, orbitSpeed * Time.deltaTime);
var desiredPosition = (transform.position - transform.parent.position).normalized * radius + transform.parent.position;
transform.position = Vector3.MoveTowards (transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
transform.position = origin + (transform.forward * (Mathf.PingPong (Time.time * zAxisDistance * zAxisSpeed + (zAxisDistance * zAxisSpeed / 2), zAxisDistance * 2) - zAxisDistance));
A better question might be, why do you need them to happen at the same time?
It’s just the nature of the app I’m writing, I need the object to move in more than one way at the same time. I know I could accomplish this by putting the object into a container object and have one script on the object moving it one way and another script on the container object moving it the other way, but bringing another object into the equation seems like a sloppy way of going about it. Having the two transform.positions in two different scripts on the same object results in the same issue of one or the other movements not happening.
You’re assigning the position twice so obviously the second assignment will always be the one that you see. It’s not super clear what you’re doing but is the solution as simple as just adding the second equation to the position instead of directly assigning it?
transform.position += origin +(transform.forward*(Mathf.PingPong(Time.time* zAxisDistance * zAxisSpeed +(zAxisDistance * zAxisSpeed /2), zAxisDistance *2)- zAxisDistance));
1 Like
9 years late, you can combine them by lerping the value separately :
Vector3 tmp;
void Update()
{
tmp = Vector3.LerpUnclamped(fromA, toA, tick);
tmp = Vector3.LerpUnclamped(Vector3.Lerp(fromB, toB, tick), tmp, tick);
transform.position = tmp;
}
you may need to use min/max there to add weighted movement