- Home /
Moving an object
I'm trying to write a small code to smootly move a cube from it's starting position to it's target position. I found a code on this website but I'm not sure how to use it.
var start : Transform;
var end : Transform;
function Update ()
{
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
This makes the object go to 0,0,0 in a matter of seconds, how do I specify the end position?
Also I would like if the object would be transparent before and after moving.
It would probably be a good idea to read a tutorial on scripting and Unity. The Walker Boys tutorials are a good start (http://www.walkerboystudio.com/html/unity_training___free__.html)
That's not useful at all, I can't be bothered watching long winded tutorials about printing in the hope I find something useful. I tried using a code they used and it gave a ton of errors, I think it's dated.
Right now I found something in the script reference:
var start : Transform; var end : Transform; function Update () { transform.position = Vector3.Lerp(start.position, end.position, Time.time); }
but it gives me the error "The variable start of 'Ghost' has not been assigned." which essentially gives me the same problem.
How do I specify the target?
In future please format code in posts using the "0100101" button in the post editor.
Also you might have better luck using function Update() ins$$anonymous$$d of the OnTriggerExit function.
It is only going to get harder trying to splice snippets of code together if you don't have a basic understanding of what is happening.
If the target is a Vector3 and not a Transform, then :
transform.position = Vector3.Lerp (transform.position, target, Time.deltaTime * 5.0);
Of course this won't mean anything to you without understanding a Transform and a Vector3.
Here is the Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
These video's are not long-winded, very short, concise and full of useful information :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
I do understand a vector3 and a transformation. But how do I specify the target?
Answer by ZenithCode · Oct 02, 2012 at 01:05 PM
Try this.
Go on your scene
Create an empty game object and place it at the end position
GO on your script where you are doing the LERP and type this code.
public Transform target;
transform.position = Vector3.Lerp(start.position, target.position, Time.time);
In the Unity editor, drag the 'target' game object onto the public field target.
Your answer
Follow this Question
Related Questions
how to make a moving object 2 Answers
Moving an object in 3D Space ~ Complexity 1 Answer
Rotate Sphere in the same place in Android touch 1 Answer
overlapping object doesn't disable 1 Answer