i have three point A,B, C. B is in between A and C. i want to move object from B to C then C to A and then A to B, how we can do.
I suggest you use Vector3.Lerp The Lerp function has 3 parameters, a startPos and endPos if you will. The third parameter is a float (between 0 and 1) that will control the position. If you have a value of 0, the startPos will be used, and 1 will return the endPos. All the values in between are interpolated by lerp, so all you need to do is to animate your progress-parameter to move your object between points.
I would recommend you to have a look at Coroutines to make this more easy for you. You can then easily animate your different sections: B->C, C->A, A->B
IEnumerator MoveObject(Vector3 startPos, Vector3 endPos)
{
float progress = 0.0f;
float speed = 0.5f;
while (progress < 1.0f)
{
_ObjectToMove.transform.position = Vector3.Lerp(startPos, endPos, progress);
yield return new WaitForEndOfFrame();
progress += Time.deltaTime*speed;
}
_ObjectToMove.transform.position = endPos;
}
This is an example coroutine, which can be used to individually animate your movement.
Cheers
Lerp is what you want basically for movement and arrays/lists for storing the path nodes. http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html Here’s a script that should help clarify what you need to do:
using UnityEngine;
using System.Collections;
public class FollowPath : MonoBehaviour {
public GameObject follower;
public Transform[] pathNodes;
public float movementSpeed = 0.5f;
public bool continuouslyLoop = false;
private float pathPosition = 0f;
private int curNode = 0;
// Use this for initialization
void Start () {
if (follower == null) {
follower = this.gameObject;
}
}
// Update is called once per frame
void Update () {
if (pathNodes != null)
{
pathPosition += Time.deltaTime * movementSpeed;
if (pathPosition > 1f)
{
if (pathNodes.Length > curNode+2)
{
pathPosition = 0f;
curNode += 1;
}
else
{
if (continuouslyLoop)
{
pathPosition = 0f;
curNode = 0;
}
}
}
follower.transform.position = Vector3.Lerp( pathNodes[curNode].position, pathNodes[curNode+1].position, pathPosition );
}
}
}