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 ?
4 Answers
4public 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.
Yes but with this options our object will passes through other object right ? But we have walls , laders and other object on way. We need know when character should jump, go down,up,right
– alek123That is quite a bit more complex. I have not worked with path finding, but it seems that is what you are looking for. http://unity3d.com/unity/quality/ai http://www.arongranberg.com/unity/a-pathfinding/ You might be able to implement some basic obstacle navigation by switching between basic movement and following cleverly placed splines, but that could potentially lead to jerky movement. Just a thought...
– cagezero
Statement :) but this is good when we have clear way but in my game we have ladders,walls and others objects.
– alek123Sorry, I didn't realise that you were using the built in pathfinding. I don't read the tags that often. Just a sec, will update my answer.
– StatementCan you tell me somthing more about this NavMesh?Any example or somthing ?
– alek123NavMesh only for pro -.- becouse we need NavMeshAgent
– alek123Well I am not sure exactly what you already have working. But basically you could just store the locations that your character has visited and go backwards when you want to go back.
– Statement