How to move a GameObject from his position to a xyz position.

How to move (translation or else) a GameObject from his position to a xyz (Vector3)?

[EDIT] Have a cube/GO that I want to move to a specific location on the terrain slowly, using Time.deltaTime . I don't want it to suddenly appear at that position.

more ▼

asked Nov 26, 2009 at 03:25 AM

user-297 (google) gravatar image

user-297 (google)
649 52 47 57

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Could you be more specific? In scripting you normally move a non-physics GameObject with the methods defined on transform. So something like:

transform.position = Vector3(10, 10, 10);

but I doubt that's what you're after.

EDIT as the next poster says, something like Vector3.Lerp would do the trick complete code in Javascript:

var endPoint : Vector3;
var duration : float = 1.0;

private var startPoint : Vector3;
private var startTime : float;

function Start() {
    startPoint = transform.position;
    startTime = Time.time;

function Update () {
    transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);
more ▼

answered Nov 26, 2009 at 03:37 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
7k 43 46 93

(comments are locked)
10|3000 characters needed characters left

The easy, built-in way is to use a Lerp function. The Lerp function takes a start and end argument, and a floating point number which you can think of as being the percentage complete of the movement. Usually this argument is based on time. So at time=0.0 Lerp will return the first number, at 1.0 it will return the 2nd number, and for numbers less than 1.0 it will return some number in-between.

The Vector3 class has a Lerp function, so you can use that to calculate points between the start and end positions.

That just leaves the question of what to pass in for the last argument. If you store the starting time (startTime = Time.time) right when the movement starts, you can pass in t = (Time.time - startTime) for the third argument. This would finish the movement after one second. To make it last twice as long, multiply t by 0.5. To make it twice as fast, multiple t by 2.

You end up with something like: transform.position = Vector3.Lerp(startPos, endPos, speed * (Time.time - startTime)); You would typically do this in your object's Update function. Every frame your object moves a bit further along.

So that's the basic approach. For fancier movement, instead of linear interpolation (Lerp) you could instead use a function that starts slow ("ease-in") and/or slows down at the end ("ease-out"). The Unity wiki has some functions that perform these kinds of effects.

more ▼

answered Nov 26, 2009 at 04:16 AM

Bampf gravatar image

5.6k 13 30 66

(comments are locked)
10|3000 characters needed characters left

What if I want the user to click on an object in 3D space and then the camera moves to a particular location? Where would I put this code?

more ▼

answered Mar 26, 2010 at 05:11 PM

Melissa gravatar image

162 10 9 15

@Melissa this is not a forum like site..so this area over here is only for answers to the above question... You can simply add a comment under any question or even better post a new question with more details. More info on how this thing works: http://answers.unity3d.com/faq

Mar 26, 2010 at 05:21 PM Lipis

hmm. search this site for something like raycast, physics raycast or mouse picking, raycast mouse picking.

Nov 16, 2010 at 08:43 AM rpl oye
(comments are locked)
10|3000 characters needed characters left

I don't think that you need it anymore , but it would be something like this:

Create a static boolean variable with its value false (static var myBool : boolean = false;) Write a code : when you click on that object myBool will get the value true and if myBool is true then Lerp the position...

script on the camera (CameraScript.js)

static var myBool:boolean=false;
var speed = 1.5;

       function FixedUpdate()
        if(myBool == true)
          transform.position = Vector3.Lerp(transform.position, waypoint.position, speed /150);

the script on the object (you want the user to click):

private var ok:boolean=false;

function OnMouseEnter() 

function OnMouseEnter() 

function Update()
   if(ok && Input.GetKey("mouse 0"))

of course you can use:

function OnMouseOver()
    if(Input.GetKey("mouse 0"))

I wrote both of them cuz' of some bad experiences in the past ...

more ▼

answered May 15, 2011 at 02:05 PM

dumytru gravatar image

83 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Nov 26, 2009 at 03:25 AM

Seen: 72415 times

Last Updated: Nov 26, 2009 at 03:56 AM