2

I am trying to combine iTween with physics acceleration.

top view of the path

Here is the top view of the path. The brown rectangle is made by a cube GameObject, with an iTweenPath component attached (4 nodes). The path is named Path1. On the left, there is a sphere (may not be seen clearly), which acts as the Actor, with RigidBody attached (mass = 3), was put few units above the floor.

Then, I assigned a script to the Actor, which is:

void OnCollisionEnter(Collision collison) {
  iTween.MoveTo(gameObject, iTween.Hash("path", iTweenPath.GetPath("Path1"), "time", 5));
}

when the ball hits the ground due to gravity, it triggers the iTween.MoveTo(). The above works as expected.

However, I want to combine with my physics acceleration script, which is:

void FixedUpdate() {
  rigidbody.AddForce(transform.forward * 5.0f, ForceMode.Acceleration);
}

However, iTween governs the movement, made the AddForce() in FixedUpdate() no effect at all. How can I combine these two?

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • So you need the sphere to follow the iTween path with applied force and no iTween movements, right? – Nick Feb 06 '14 at 05:59
  • Yes. the sphere should follow the path, but its speed should be set according to the force applied to it. – Raptor Feb 06 '14 at 06:01
  • 1
    ok let me try this and let you know.. – Nick Feb 06 '14 at 06:12
  • do you want to use iTween path only? Or with only empty gameobject will also do? – Nick Feb 06 '14 at 06:38
  • Any solution is welcome. My goal is to allow my character to run in curve and its speed is according to physics acceleration (there are crouching & jumping actions too). Just like Temple Run. – Raptor Feb 06 '14 at 07:10

1 Answers1

1

I have not used iTween in this solution. Just used number of empty gameobjects in array to generate path. More gameobjects in path smoother the path.

using UnityEngine;
using System.Collections;

public class RollBall : MonoBehaviour 
{
    public float speed=10;
    public Transform target;
    public Transform[] pathPoints;
    public int c;


void Start () 
    {
        c=0;
    }
    
    void Update () 
    {
        target.position=new Vector3(transform.position.x,transform.position.y+1f,transform.transform.position.z);
        if(target.position.z>=pathPoints[c].position.z)
        {
            if(c<pathPoints.Length)
                c++;
        }
        target.LookAt(pathPoints[c]);

        if(Input.GetKey(KeyCode.UpArrow))
        {
            rigidbody.AddForce(target.forward*speed);
        }
    }

}

In this target is the empty gameobject which moves with the sphere to give direction to the sphere. pathPoints is the array of empty gameobjects generating a curved path (sequence does matters). Try this code with attaching it to a sphere. Let me know here if any doubts.

Community
  • 1
  • 1
Nick
  • 1,035
  • 1
  • 11
  • 18
  • 1
    I got your point. By using `target.LookAt()` and applying forward force, the character will move towards a hidden path made by a series of empty game objects. Nice solution! – Raptor Feb 06 '14 at 07:26