- Home /
Make an object follow a curved path using iTween
Hi, I have asked a few questions now and have gotten responses for all so i am grateful and hope this trend continues. My current question is in regards itween. Say you have a cube at (0,0,0). How do you make it travel along a curved path? Bear in mind moveToBezier is deprecated in 2.0. A code snippet would be much appreciated. Thank you all in advance.
Hellooooo....? Are you still there? Only you can close the case!
Hi Shrandis, thanks for your answer. But if i was to be more detailed with my requirements then I would have to also add that i need the curved path to be a different random curved path each time(how would you come up with the waypoint coordinates??). Also the curve has to have easing, which i don't think putonpath allows. Do you know how to do this?? But nevertheless i think you answered my original question. Thanks again.
Hmm, I do not know if you can have that much control over the path with iTween. You may need to write a path system yourself.
Glad if I could help, don't forget to press the green tick & thumbs up buttons if the answer helps you.
Answer by Shrandis · Oct 03, 2012 at 10:29 AM
You can use iTween's PutOnPath function.
Simple example:
Create "waypoint" gameobjects. (empty gameobjects). Then, assign those waypoints in the order you want to waypointArray.
public Transform[] waypointArray;
float percentsPerSecond = 0.02f; // %2 of the path moved per second
float currentPathPercent = 0.0f; //min 0, max 1
void Update ()
{
currentPathPercent += percentsPerSecond * Time.deltaTime;
iTween.PutOnPath(gameObject, waypointArray, currentPathPercent);
}
void OnDrawGizmos()
{
//Visual. Not used in movement
iTween.DrawPath(waypointArray);
}
Keep in mind that PutOnPath may have some issues if your goal is to make a movement with constant speed. (I'm not going into detail because it isn't what OP asked)
This is actually perfect for what I was looking for myself, however how would I go about making the gameobject turn/face the way it's moving also to add a jump feature?
I have this setup if it helps;
using UnityEngine;
using System.Collections;
public class $$anonymous$$ovement : $$anonymous$$onoBehaviour {
private float smoothing = 1.0f;
private Vector3 targetPosition;
public float runSpeed = 0;
private int jumpCount = 0;
public float jumpForce = 1.0f;
public float gravity = 10;
public Transform[] path;
float percentsPerSecond = 0.02f; // %2 of the path moved per second
float currentPathPercent = 0.0f; //$$anonymous$$ 0, max 1
// Use this for initialization
void Start () {
Physics.gravity = new Vector3(0, -gravity, 0);
}
// Update is called once per frame
void Update ()
{
bool grounded;
currentPathPercent += percentsPerSecond * Time.deltaTime;
iTween.PutOnPath(gameObject, path, currentPathPercent);
//JU$$anonymous$$P//
if (Physics.Raycast (transform.position, -transform.up, 1))
{
grounded = true;
//reset our jump count since we hit the ground //0 for double jump
jumpCount = 1;
}
else
{
grounded = false;
float amtTo$$anonymous$$ove = Input.GetAxisRaw ("Horizontal") * 0.1f * Time.deltaTime;
}
if (Input.Get$$anonymous$$eyDown ("space") && (grounded || jumpCount < 1))
{
rigidbody.AddRelativeForce (transform.up * jumpForce, Force$$anonymous$$ode.Impulse);
}
}
void OnDrawGizmos(){
iTween.DrawPath(path);
}
}
Sounds like you got pretty far with it! how did this turn out?
Answer by Koyemsi · Sep 12, 2019 at 11:08 AM
Replying to an old thread, but some may find this useful...
One can also use iTween.MoveTo() with a "path" attribute. The path has to be an array of Transform or Vector3.
Here is an example :
public GameObject movingObject;
public Transform[] waypoints;
float speed = 2f;
iTween.MoveTo(movingObject, iTween.Hash(
"path", waypoints,
"time", speed,
"easetype", iTween.EaseType.easeInOutQuad));
Your answer
![](https://koobas.hobune.stream/wayback/20220613081738im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using iTween.PutOnPath 1 Answer
How to display a bezier curve between transforms in-game rather than in editor? 2 Answers
Can iTween be used to constrain motion for an arbitrarily drawn curve? 1 Answer
iTween for Dynamically Moving Objects around a Sphere 0 Answers
Itween for curved orbital movement? 1 Answer