2015年6月16日火曜日

iTweenパス上を前を向きながら移動するサンプル

iTweenパス上を前を(進行方向を)向きながら移動するサンプル。





環境

Unity 5.0.1f1 Personal.
iTween Visual Editor.
※Asset Storeからインポート。




概要

乗り物等の移動物をちょっと動かす際に便利。
移動経路の作成にはAsset Store にある iTween Visual Editor を使用。
 (参考: iTweenパス上をユーザー操作で移動するサンプル) 


結論から言ってしまうと、

iTween.MoveTo(gameObject, table);

で、引き渡す table に、

table.Add( "orienttopath", true );
table.Add( "lookTime", 1.0f );

を追加するだけでできてしまう。

lookTime で与える数値で姿勢転換の強度を指定。
  • 小さい値: 姿勢転換が早くなる。
  • 大きい値: 姿勢転換が緩やかになる。

姿勢転換の挙動は移動パスのカーブの孤の大きさや、移動速度(time)にも関係するので要調整。

移動するオブジェクトの向き(XYZ)と姿勢の関係は、
  • Z+: 前
  • Y+: 上
  • X+: 右
となっているので、予めこのようにモデリング/エクスポートしておくか、空のGameObjectを作って中に放み込み&向き調整等で。




サンプル


サンプルソース
-----
/*
 * @file csMoveHeadingToPath.cs
 * @note  None 
 * @attention 
 *    [csMoveHeadingToPath.cs]
 *    Copyright (c) [2015] [Maruton]
 *    This software is released under the MIT License.
 *    http://opensource.org/licenses/mit-license.php
 */
using UnityEngine;
using System.Collections;

public class csMoveHeadingToPath : MonoBehaviour {
 string pathName = "MyPath";
 float pathTime = 18.0f;//5.0f
 Hashtable table = new Hashtable();
 void SetupPath(){
  table.Add( "path", iTweenPath.GetPath(pathName) );//ITween path hash data
  table.Add( "time", pathTime );
  table.Add( "easetype", iTween.EaseType.easeInOutSine );
  table.Add( "onstart", "cb_iTweenStart" );  //Handler func when iTween start
  table.Add( "onstartparams", "Start" );   //parameter of Handler func when iTween start
  table.Add( "oncomplete", "cb_iTweenComplete" ); //Handler func when iTween end
  table.Add( "oncompleteparams", "Complete" ); //parameter of Handler func when iTween end
  
  table.Add( "orienttopath", true ); //Head to forwarding vector (*Important*)
  table.Add( "lookTime", 1.0f ); //Time value for heading nose (*Important*)
  
  iTween.MoveTo(gameObject, table);
 }
 void cb_iTweenStart(string param){
  Debug.Log("[iTween] cb_iTweenStart: "+param);
 }
 void cb_iTweenComplete(string param){
  Debug.Log("[iTween] cb_iTweenComplete: "+param);
  iTween.MoveTo(gameObject, table);//Restart iTween.
 }

 void Start () {
  SetupPath();
 }
 void Update () {
 }
}
-----

Unity5用プロジェクトのダウンロード。
Git-Hub:https://github.com/maruton/Sample_iTween_HeadingPath



0 件のコメント:

コメントを投稿