Traslating a position of an object to another, smoothly?

I have a camera with ‘Smooth Follow’ attached. It’s looking at an object called ‘Camera Target’. Camera Target has a script attached to move it around based on XYZ coordinates. The script below seems to move it from point A to B, but what I would like is to smoothly transition over time to each position. There’s ‘transform.Translate’, but looking at the reference manual I can’t figure out how to give it specific coordinates. What’s a better way to code this:

transform.position = Vector3(0, .75, 0);

function OnGUI () {
if (GUI.Button (Rect (10,10,90,25), “Head”)) {
transform.position = Vector3(0, 1.2, 1.55);
}
if (GUI.Button (Rect (10,40,90,25), “Upper Torso”)) {
transform.position = Vector3(0, 1, 1.325);
}
if (GUI.Button (Rect (10,70,90,25), “Lower Torso”)) {
transform.position = Vector3(0, .51, 1);
}
if (GUI.Button (Rect (10,100,90,25), “All”)) {
transform.position = Vector3(0, .75, 0);
}
}


Thanks!

did you try coordinates * Time.deltaTime?
If i were you, i specified speed first, so i can change/manipulate it on runtime, where needed, so there is no need to recompile code to tweak it.
after that i would give names for positions, just in case model has been moved or rotated, so they updates in real time also and i don’t have to worry about calculations.

Not sure what your project is all about, but looks like you just want your camera to fly around looking at your model.

First of all, remember, you are working with Space.Self (local axis), not Global
at the moment you are telling it to “teleport” to those positions, you need however tell code to reduce distance between (subtraction) multiplied by delta time.

// hardcoding tansform positions is a BAD IDEA, but it's your code and question was different
var head : Vector3 = Vector3(0, 1.2, 1.55);
var upperTorso : Vector3 = Vector3(0, 1, 1.325)
var lowerTorse : Vector3 = Vector3(0, .51, 1);
var all : Vector3 = Vector3(0, .75, 0);

// now lets set up smooth and speed
var speed : int = 0;
var smooth : float;

function Update () {
smooth = speed * Time.deltaTime;
}

function OnGUI () {
if (GUI.Button (Rect (10,10,90,25), "Head")) {
transform.Translate (transform.position - head.position * smooth);
}
if (GUI.Button (Rect (10,40,90,25), "Upper Torso")) {
transform.Translate ( transform.position - upperTorso.position * smooth);
}
if (GUI.Button (Rect (10,70,90,25), "Lower Torso")) {
transform.Translate (transform.position - lowerTorso.position * smooth);
}
if (GUI.Button (Rect (10,100,90,25), "All")) {
transform.Translate ( transform.position - all.position * smooth);
}
}

i wrote this code here in quick reply, did not test, just wanted to show you how it’s done
basically to move it smoothly:

  1. tell unity current position in 3D space (vector3) and that is transform.position
  2. tell it target position, and that is also Vector3, .position. However they are hardcoded by you. So if model moves somewhere or rotates in local space, transforms will also change. Solution would be adding 4 empty game objects as child objects to your model for your camera to Transform.LookAt; Simple as that :slight_smile: Where ever parent moves, child will move too. So you’d just define those game objects like:
var head : Transform;
var upperTorso : Transform;
var lowerTorse : Transform;
var all : Transform;

Then you simple use unity editor to drag and drop empty game objects into scripts transform values and you are done. This way you can always adjust game objects if you want to manipulate camera later for tweaks
3) you move camera every frame multiplied by “last known time” as in - delta time. This way no matter what FPS your PC runs on, it will move all the same. Also now you can manipulate smooth speed on runtime by single variable
Basically you are moving object by subtracting distance variable from current position to target position, multiplied by smooth or speed every frame.
4) check out Mathf in script references. I believe it’s most powerful tool to manipulate any values in Unity

EDIT: sry fixed transform.Translate

I’m creating a Character Creator. So I would like the camera to zoom to each component of the mesh, to get a better view. For example; you’re changing hair styles or facial features, so the camera would move closer to the head to see the style and then you can make adjustments accordingly. So depending on what you’re changing, the camera would smoothly “bounce around”.

I’m really new to coding, so please bare with me! Well I took your code (made some minor adjustments such as a missing ; and respelling Torse to Torso), but I’m having 4 compiling errors:

Assets/Scripts/Camera Buttons 2.js(17,48): BCE0019: ‘position’ is not a member of ‘UnityEngine.Vector3’.

Any thoughts on that? Thanks so much for your help!!

gah, that is weird. This is what i hate about it - some minor mystical crap always jumps out…
and .position is a member of Vector3. Look, from script Reference:

Anyway, simply remove head, torso and all .positions from GUI script and it will compile :slight_smile:

function OnGUI () {
if (GUI.Button (Rect (10,10,90,25), "Head")) {
transform.Translate (transform.position - head * smooth);
}
if (GUI.Button (Rect (10,40,90,25), "Upper Torso")) {
transform.Translate ( transform.position - upperTorso * smooth);
}
if (GUI.Button (Rect (10,70,90,25), "Lower Torso")) {
transform.Translate (transform.position - lowerTorso * smooth);
}
if (GUI.Button (Rect (10,100,90,25), "All")) {
transform.Translate ( transform.position - all * smooth);
}

apparantly you can only do .position with transform. only

btw i am also very new to coding, my personal secret is pressing F1 on each of code alot and read read read :slight_smile:

also a side note, check out iTween for Unity. It’s free. Search forums for code-less iTween visual manager too (just search for iTween). You will be blown away by those scripts, few videos on their website. Check out examples, they have spline tools and what not for your exact needs.
I do not support that, because it is not related to this thread’s question, and it’s a lazy way of doing it, coding yourself is the way to learn. But i could be wrong and looking at iTween’s code could speed up your learning quite alot…

-Ray

Thanks Ray, I’ll take a look at iTween also.

For some reason when I click on the buttons it doesn’t move the ‘Camera Target’ to the appropriate XYZ coordinates, it just keeps moving it up along the Y axis. Anyways I’ll try your F1 trick and see if I can figure it out.

Thanks for all your help!