Last active
January 10, 2017 02:31
-
-
Save tetya/1df308b704dba11f111b9ba61e16b182 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class FollowingCocpit_2 : MonoBehaviour { | |
private Transform _followedTarget;//<! 追従対象 | |
private Transform _cockpitPosTra;//<! コクピット収納位置 | |
private Transform _dummyTarget;//<! 追従対象のダミー(ダメージモーション等のとき、本来の頭の位置に設置する) | |
private Transform _player; | |
private float _t = 0f; | |
[SerializeField] private float _limitRot = 0f; | |
[SerializeField] private float _accelerationInLimitRot = 5f; | |
[SerializeField] private float _rotSpeed = 1f; | |
[SerializeField] private float _posSpeed = 0.3f; | |
void Start() | |
{ | |
// プレイヤーからアクセスできるよう自身を登録 | |
_player = GameObject.FindWithTag("Player").transform; | |
_player.GetComponent<MSManager>().SetCockpitFollowing(this); | |
// コクピット収納位置 | |
_cockpitPosTra = _player.GetComponent<MSManager>().msData.GetCockpitPositionTransform(); | |
transform.position = _cockpitPosTra.position; | |
// 最初はコクピット収納位置に追従 | |
_followedTarget = _cockpitPosTra; | |
//ダミーを複製しておく | |
_dummyTarget = Object.Instantiate(_cockpitPosTra.gameObject).transform; | |
_dummyTarget.name = _cockpitPosTra.gameObject.name + " Dummy"; | |
JoinToRealHead(); | |
} | |
void Update() | |
{ | |
if (_followedTarget == null) return; | |
ControlRotation(); | |
ControlPosition();//アニメーション適用後なので、IKにより位置が変わると差異が生まれる | |
Debug.DrawLine(_dummyTarget.position, _followedTarget.position,Color.red); | |
} | |
void ControlPosition() | |
{ | |
if (transform.position == _cockpitPosTra.position && _t == 1) return; | |
_t = Mathf.Clamp01(_t + Time.deltaTime * _posSpeed); | |
Debug.Log(_t); | |
Vector3 goal = Vector3.Lerp(_dummyTarget.position, _cockpitPosTra.position, _t);//_dummyTarget.position が _cockpitPosTra.positionと同値の場合、_tは無関係 | |
transform.position = goal; | |
} | |
void ControlRotation() | |
{ | |
//回転の追従 | |
float angle = Quaternion.Angle(transform.rotation, _followedTarget.rotation); | |
float amp = 1 + Mathf.InverseLerp(_limitRot, 180, angle) * _accelerationInLimitRot; | |
transform.rotation = Quaternion.Slerp(transform.rotation, _followedTarget.rotation, Time.deltaTime * _rotSpeed * amp); | |
} | |
//! Animatorから呼び出す | |
public void LeaveFromRealHead() | |
{ | |
_t = 0; | |
if (_followedTarget == _dummyTarget) return; | |
Debug.Log("Leave"); | |
//! 呼ばれた時点での頭の座標と向きをコピー | |
_dummyTarget.position = _cockpitPosTra.position; | |
_dummyTarget.rotation = _cockpitPosTra.rotation; | |
//親を変える | |
_dummyTarget.parent = _player.transform; | |
//! 追従対象を変える | |
_followedTarget = _dummyTarget; | |
} | |
//! Animatorから呼び出す | |
public void JoinToRealHead() | |
{ | |
Debug.Log("Join"); | |
//! 呼ばれた時点での頭の座標と向きをコピー | |
_dummyTarget.position = _cockpitPosTra.position; | |
_dummyTarget.rotation = _cockpitPosTra.rotation; | |
//親を変える | |
_dummyTarget.parent = _cockpitPosTra.transform; | |
//! 追従対象を変える | |
_followedTarget = _cockpitPosTra; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment