- Home /
Itwen MoveTo - jump
Hello everyone. I'm using ITween, iTweenPath to make a car following a path. I want the car jump when you press the space button, but I can not. Can anyone help?
void Start () {
iTween.MoveTo(gameObject, iTween.Hash("path", iTweenPath.GetPath("pathteste"), "time", 10,"easetype",iTween.EaseType.linear, "orienttopath", true));
}
Answer by robertbu · Aug 02, 2014 at 08:17 PM
One solution is to make the visual car a child of an empty game object at the same position. You use the empty game object in the iTween call. You implement the jump in a separate script attached to the car that changes the car's transform.localPosition. Something like:
#pragma strict
public var jumpHeight : float = 0.75;
public var jumpTime : float = 0.8;
private var jumping = false;
function Update () {
if (!jumping && Input.GetKeyDown(KeyCode.Space)) {
Jump();
}
}
function Jump() {
jumping = true;
var timer : float = 0.0;
while (timer <= jumpTime) {
var height : float = Mathf.Sin(timer / jumpTime * Mathf.PI) * jumpHeight;
transform.localPosition = Vector3(0, height, 0);
timer += Time.deltaTime;
yield;
}
transform.localPosition = Vector3.zero;
jumping = false;
}
It worked but not exactly. The object follows the path but when I press the button, it disappears from where it is, appears at the starting point, jumps, disappears from the start point and appears again where you pressed the button and continue the path. I do not know if what I said was clear but that is what is happening
Thank you very much. Now it works perfectly. Just did a change in the code:
while (timer <= jumpTime) {
float height = $$anonymous$$athf.Sin(timer / jumpTime * $$anonymous$$athf.PI) * jumpHeight;
transform.localPosition = new Vector3(transform.localPosition.x, height, 0);
timer += Time.deltaTime;
yield return 0;
}
There is some setup error if you are having the problem you claim. The most likely problem is that you did not setup the empty game object and the visible game object to be in the same place. That is, the position of your visible, child object should be (0,0,0) in the editor since the editor shows the local position of an object. And then you need to revert to my code. In addition you should use:
yield return null;
...for C#.
Your answer
Follow this Question
Related Questions
The player doesn't jump to the left with the W key pressed 0 Answers
integrating itween with physics 1 Answer
How to make objects "Bounceable" 1 Answer
Problem with joints 2 Answers
Wall Jump Scripting Help 2 Answers