We have two object one stay on current position but second can move, when we press button this second will back in this same way. Is any easy way to do that ?
public var OBJECT: GameObject;
public var MoveOnX : float;
public var MoveOnY : float;
public var MoveAmount : float = 1;
public var MoveSpeed : float = 2; //change to edit the ammount of time it takes to go back to default position
public var DefaultPos : Vector3;
public var NewPos : Vector3;
function Start(){
DefaultPos = transform.localposition;
}
function Update () {
MoveOnX = Input.GetAxis("Mouse X") * Time.deltaTime * MoveAmount;
MoveOnY = Input.GetAxis("Mouse Y") * Time.deltaTime * MoveAmount;
NewPos = new Vector3 (DefaultPos.x+MoveOnX, DefaultPos.y+MoveOnY, DefaultPos.z);//reset to original position
OBJECT.transform.localPosition = Vector3.Lerp(OBJECT.transform.localPosition, NewPos, MoveSpeed*Time.deltaTime);//make object go there
if( Input.GetButtonDown("BUTTONYOUWANTCLICK")){
NewPos = new Vector3 (DefaultPos.x+WHATEVERVALUE, DefaultPos.y+WHATEVERVALUE, DefaultPos.z+WHATEVERVALUE);//change these to create your new position
OBJECT.transform.localPosition = Vector3.Lerp(OBJECT.transform.localPosition, NewPos, MoveSpeed*Time.deltaTime);//make object go there
}
}
Hope this helps
Looks like you can get the path with this API:
Then you get a NavMeshPath. You can use it to find all the locations you need to visit and thus you should be able to navigate backwards as well by sampling the positions.
http://docs.unity3d.com/Documentation/ScriptReference/NavMeshPath.html
Statement but this is good when we have clear way but in my game we have ladders,walls and others objects.
Two of many techniques for doing this:
GameObject object1;
GameObject object2;
float speed = 10;
void Update() {
//Do it immediately
if (Input.anyKeyDown)
ReturnToObject();
//Do it slowly
if (Input.anyKeyDown)
StartCoroutine(ReturnToObject1());
}
//Do it immediately
void ReturnToObject() {
object2.transform.position = object1.transform.position;
}
//Do it slowly
IEnumerator ReturnToObject1() {
while(object2.transform.position != object1.transform.position) {
object2.transform.position = Vector3.MoveTowards(object2.transform.position, object1.transform.position, Time.deltaTime * speed);
yield return null;
}
}
Cache the transforms to make it more efficient.