moving from one place and returning back

Hi i would like to see a moving from one place and returning back code because all the tutorials that i am finding are all to follow. So could some one please help me with the code or if you know some tutorials that involve in an object to move to one place and come back. I was trying to maybe do it with event trigger but failed and then was trying with the example ( vector3(3,3,3) ) but it doesn’t come back.

I am afraid you are going to have be be a little more specific and maybe add some code. (remember to use code tags)

As far as I am seeing, you want Vector3.MoveTowards().

Using Lerp, Slerp or SmoothDamp will never reach your goal.

using UnityEngine;
using System.Collections;

public class TestBed : MonoBehaviour
{

	public float speed = 3;
	public Vector3 target = Vector3.zero;
	private Vector3 origin;

	void Start(){
		origin = transform.position;
	}

	void Update(){
		transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
		if (transform.position == target) target = origin;
	}
}

This basically moves the object frame by frame at speed per second until it reaches the desired location. When the object reaches that location, it simply changes the target back to the origin and starts moving again.

You can spiffy this up some by rotating your object to go in that direction:

	void Update(){
		if (transform.position != target) transform.LookAt (target);
		transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
		if (transform.position == target) target = origin;
	}

And, of course, since I am not making you be really specific, this is of course NOT what you wanted. lol

Wow it worked and its exactly what i wanted. Thank you very much. and if i might ask something else, if i wanted to make it work when a key button or gui button is clicked, do i have to put this code somewhere else other than update or if it isn’t in update it won’t work?

Yes and no, in this case. There are multiple ways to attack that situation. (to include but not limited to:)

  1. store a value which says that it should move. In your Update/OnGUI when the player click’s the button/key set the one that says it should be moving to true, but only if it is not moving. In your update, then move your object only if, it should be moving. When transform.position == origin, set should be moving back to false. (thus allowing the player to change the value again.)

  2. use a variable that states if it is moving. If it is not, allow the key/button input just as above. When the button/key is clicked, start a coroutine that first sets the state of moving to true, then does a yield return 0; (which mimics the Update) and do all your movement there. When transform.position == origin, stop the while and set is moving back to false.

I am sure people will come up with a million and one ways to do this, it is not complex code.

hi, i want to make an object go to a place with delay so with a type like delta time and then come back

if (GUI.Button (new Rect (Screen.width / 2, Screen.height / 2, 200, 300), “Attack”))
{
attack1clicked = true;

}

if (attack1clicked == true )
{
transform.LookAt (EnemyCreature);
transform.position = (Vector3(1,1,1) * Time.deltaTime);
}

only this is working but i can’t find how to make it stop, do something and then go back

transform.Translate (Vector3.forward * Time.deltaTime);

and when i try this so when i click the button it only works once because i made another
or for example if it is at x,y,z it stops do a script like play animation and do damage and then go back

the code that you gave me worked but if i put it on a gui button it only works once even with

if (GUI.Button (new Rect (Screen.width / 2, Screen.height / 2, 200, 300), “Attack”))
{
attack1clicked = true;
}

and i was trying to make it false with the turns

timeleft -= Time.deltaTime;

//to stay 0
if (timeleft <= 0.0f) {
timeleft = 0.0f;
}

//to go turn 2
if (theturn == 1 timeleft == 0.0f) {
timeleft = 10.0f;
theturn = 2;

attack1clicked = false;

}

//to go turn 1
if (theturn == 2 timeleft == 0.0f) {
timeleft = 10.0f;
theturn = 1;
attack1clicked =false;
}

both are false but i did it like that so that the i have to press the gui button again but i only works once and when i made a debug.log it gave me a message. i know i a little bit new to C# but im stuck because there are no video tutorials about things like this.