- Home /
Object move back to first position
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 ?
Answer by Statement · Dec 20, 2012 at 08:02 PM
Looks like you can get the path with this API:
http://docs.unity3d.com/Documentation/ScriptReference/NavMeshAgent.CalculatePath.html
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.
Sorry, 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.
Can you tell me somthing more about this Nav$$anonymous$$esh?Any example or somthing ?
Nav$$anonymous$$esh only for pro -.- becouse we need Nav$$anonymous$$eshAgent
Well 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.
Answer by JaydenM1997111 · Dec 21, 2012 at 12:53 AM
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 :)
Answer by alek123 · Dec 21, 2012 at 12:53 AM
Statement :) but this is good when we have clear way but in my game we have ladders,walls and others objects.
Answer by cagezero · Dec 20, 2012 at 08:12 PM
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
That 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...
Your answer
Follow this Question
Related Questions
Pathfinding and AI for an RTS game 3 Answers
AI Pathfinding In A Certain Height 1 Answer
A* Pathfinding AIFollow in JavaScript? 0 Answers
NavMesh.SamplePosition returns point outside the NavMesh 0 Answers
"E" to grab an object... 3 Answers