- Home /
iTween path using self object as path0
Im trying to move an instantiated prefab using itween and I want that the first path is itself, for example, move it to another point using iTween.
I tryied many ways, my last try is I used the object as Vector3,
Take a look at my code:
public Transform[] path;
void Start()
{
Vector3 position1 = new Vector3(transform.position.x, transform.position.y, transform.position.z);
path[0] = position1;
}
void OnMouseDown ()
{
if(transform.gameObject.tag == "Player")
{
iTween.MoveTo(gameObject, iTween.Hash("path", path,"axis", "-x", "movetopath", false, "orienttopath", true,"speed", 100));
}
}
}
The error was that:
error CS0029: Cannot implicitly convert type UnityEngine.Vector3' to
UnityEngine.Transform'
Answer by robertbu · Apr 01, 2014 at 06:56 PM
You can do it this way:
void Start() {
var t = new GameObject().transform;
t.position = transform.position;
path[0] = t;
}
Note I'm assuming that you are creating a path in the Inspector. If the size of 'path' is 0 when Start() is executed, you will get an out of bounds error.
It don't appeared on the Inspector, don't worked, I'm Instantiating the object so I cant only drag the path to it
This is not the whole script. This is the replacement for your Start() function in your script above.
Answer by Fred_Vicentin · Apr 01, 2014 at 08:41 PM
I done by myself:
Its working, thanks for the help
public Transform fruta;
public Transform mao;
public Transform[] path;
void Start()
{
fruta = gameObject.transform;
mao = GameObject.Find("Mao").transform;
path[0] = fruta;
path[1] = mao;
}
This will compile, but will it run right? Transforms are by reference, so you will be changing the position of 'path[0]' each frame effectively shortening the path.
Yes, it worked right for me, I used with iTween and worked as I want to
Your solution here has a potential problem based on my belief at how iTween functions. It re-evaluated the path each frame. You are putting the transform of your game object in the path. The result is that the path is changing each frame...shortening as the position of your game object moves closer to the goal. I believe this means two things. First, if you were to clock your object across the path, you would find the movement is not linear. This may be hidden in part by whatever the default tween is for iTween movement. Second, you could not move back across the same path because you lose the starting point.
There is one other strange/potential problem with what you've done here. 'path' has to have its length set to 2 or more in the Inspector. Typically if the programmer was not going to set the path in the Inspector, he would make the variable private and initialize 'path' in start:
path = new Transform[2];
Actually given how you are using 'path', making 'path' and array of Vector3s might be better.
Your answer
Follow this Question
Related Questions
ITween orienttopath x-y-z? 1 Answer
iTween makes a weird start on the path 1 Answer
How do you create random paths in iTween? 0 Answers
how to cancel iTween Catmull-Rom path 1 Answer
iTween even velocity 9 Answers