- Home /
How to create a Path
Hey guys I need some Help here. How to create a path for example to make a path in form of an 8 and when I am moving my character it will go only on this path.
I don't want it to move automatic or to follow it, I just need to it make when I press D it will on this path and when I press A will move opposite direction.
My thought is to make an array of points, where this points will be the path but I don't know how to implement it, that's why I'm looking for your help.
Answer by robertbu · Mar 16, 2014 at 07:42 PM
The easiest solution is to use iTween (free in the AssetStore). If you want to roll your own, search UA for 'waypoint' answers or 'spline' answers. Code for both has been published many times.
Note iTween has a free path editor:
http://pixelplacement.com/2010/12/03/visual-editor-for-itween-motion-paths/
You can use iTween's built in functions to move along the path or you can use iTween.PutOnPath() and/or iTween.PointOnPath() and manage the movement in your own code.
This is my version of doing with iTween but by now i just managed to make it follow automatic . This is my code .
public float maxSpeed = 10f;
public Transform[] waypointArray;
float percentsPerSecond = 0.1f; // %2 of the path moved per second
float currentPathPercent = 0.0f; //$$anonymous$$ 0, max 1
void Update ()
{
Vector3 move = Vector3.zero ;
//Get the acceleration from x,y by accelerometor
move.x= Input.acceleration.x;
move.y = Input.acceleration.y;
//Apply Transation to the gameObject,
//Space.World - Applies transformation relative to the world coordinate system.
transform.Translate(Vector3.right * (move.x * maxSpeed) * Time.deltaTime,Space.World);
transform.Translate(Vector3.up * (move.y * maxSpeed) * Time.deltaTime,Space.World);
currentPathPercent += percentsPerSecond * Time.deltaTime;
iTween.PutOnPath(gameObject, waypointArray, currentPathPercent);
}
Can you help me to make it move not automatic? but when I am moving my device?
Your use of this 'acceleration' hints that you want a more complex motion than your specific question. And your 'Translate' calls will be overridden by your 'PutOnPath()' call. You are attempting to use both x and y of the acceleration, but a path only as two directions...forward and backward. And your acceleration will measure changes in velocity, so you cannot easily map that into a direction since the values will always balance out and return to zero. So I'm not sure what you want here. $$anonymous$$aybe you want the gyroscope ins$$anonymous$$d?
No, it must be with acceleration.
Here is a quick pic of what I am doing. The particles are showing my path.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140415im_/https://answers.unity.com/themes/thub/images/avi.jpg)