- Home /
Lerp a Object along X and Z axis only.
Im new to Unity (still). Sorry if this is a .. stupid question ..
What I want to know, is how/with what you can make a gameobject transform to another postition in space in straight lines.
Here is a image of what I mean.
I know how lerp would work, but that seems to take the most direct way.
Answer by hypnoticmeteor · Jan 08, 2015 at 09:38 PM
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Vector3 startPos;
public Vector3 endPos;
public GameObject gameObjectOne;
float angleT;
float distanceH;
Vector3 pointThree;
float x;
bool moveObjectPosOne,moveObjectPosTwo;
public int speed;
IEnumerator Start () {
moveObjectPosOne = false;
moveObjectPosTwo = false;
yield return StartCoroutine(CalculateVariables());
}
double DistanceBetweenPoints(Vector3 vectorOne, Vector3 vectorTwo) {
double distance = Vector3.Distance(vectorOne, vectorTwo);
return distance;
}
float GetAngleBetweenPoints(Vector3 vectorOne, Vector3 vectorTwo)
{
float xDiff = vectorTwo.x - vectorOne.x;
float yDiff = vectorTwo.z - vectorOne.z;
return Mathf.Atan2(yDiff, xDiff) * (180 / Mathf.PI);
}
float CalculateX(float angle, float distance)
{
float x = distance * Mathf.Cos(angle);
return x;
}
IEnumerator CalculateVariables() {
gameObjectOne.transform.position = startPos;
angleT = GetAngleBetweenPoints(startPos, endPos);
distanceH = (float)DistanceBetweenPoints(startPos, endPos);
x = CalculateX(angleT, distanceH);
pointThree = new Vector3(startPos.x - x, startPos.y, startPos.z);
yield return null;
}
// Update is called once per frame
float VecOne;
float VecTwo;
void Update () {
VecOne = Vector3.Distance(gameObjectOne.transform.position,pointThree);
if (moveObjectPosOne) {
gameObjectOne.transform.position = Vector3.MoveTowards(gameObjectOne.transform.position, pointThree, speed * Time.deltaTime);
if (VecOne <= 0)
{
moveObjectPosOne = false;
moveObjectPosTwo = true;
}
}
if (moveObjectPosTwo)
{
gameObjectOne.transform.position = Vector3.MoveTowards(gameObjectOne.transform.position, endPos, speed * Time.deltaTime);
}
}
}
Sixth grade algebra. I have cooked this up real quick. Not sure if this would work exactly but you can start here. Hopefully I am not doing homework. :D
Answer by jpthek9 · Jan 08, 2015 at 07:33 PM
Make a queue of positions to lerp to then lerp to the next position if the object reached the last position. There's not much of a way to do this except with hard coding and lots of plumbing code.
The good part is that the solution is intuitive so no fancy maths involved.
Answer by DanSuperGP · Jan 08, 2015 at 07:50 PM
Lerp is a linear interpolation, so it will always travel along the line between two locations.
If you want to only travel along the y axis first, then travel along the x axis... you need to split it up into two motions. One that does the y motion, and another that does the x motion once the y motion is completed.
There isn't a built in way to do it, you're going to have to write your own.