5

Unity3D を使用しています。マウス ポインターの方向を向くようにオブジェクトを回転させたいのですが、「毎秒最大 100 度」のような最大回転速度を許可します。

ドキュメントに例がありますが、それは私が望むことをしません。
Time.time は Time.deltaTime であるべきだと思いますが、最後のパラメーターが何をするのかよくわかりません。開始ベクトルに合計される数値になるはずですか?
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html

また、最後のパラメーターが何をするのか本当に理解できません。ローテーションの時期でしょうか。

今使っているコード

Plane plane = new Plane(Vector3.up, 0);
float dist;
void Update () {
    //cast ray from camera to plane (plane is at ground level, but infinite in space)
    Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
    if (plane.Raycast(ray, out dist)) {
        Vector3 point = ray.GetPoint(dist);

        //find the vector pointing from our position to the target
        Vector3 direction = (point - transform.position).normalized;

        //create the rotation we need to be in to look at the target
        Quaternion lookRotation = Quaternion.LookRotation(direction);

        //rotate towards a direction, but not immediately (rotate a little every frame)
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
    }
}

弱点はSlerpの3番目のパラメータだと思うのですが、そこに何を入れればいいのかわかりません。

4

2 に答える 2

4

このコードは機能しますが、100% 正しいかどうかはわかりません。

Plane plane = new Plane(Vector3.up, 0);
float dist;
void Update () {
    //cast ray from camera to plane (plane is at ground level, but infinite in space)
    Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
    if (plane.Raycast(ray, out dist)) {
        Vector3 point = ray.GetPoint(dist);

        //find the vector pointing from our position to the target
        Vector3 direction = (point - transform.position).normalized;

        //create the rotation we need to be in to look at the target
        Quaternion lookRotation = Quaternion.LookRotation(direction);

        float angle = Quaternion.Angle(transform.rotation, lookRotation);
        float timeToComplete = angle / rotationSpeed;
        float donePercentage = Mathf.Min(1F, Time.deltaTime / timeToComplete);

        //rotate towards a direction, but not immediately (rotate a little every frame)
        //The 3rd parameter is a number between 0 and 1, where 0 is the start rotation and 1 is the end rotation
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, donePercentage);
    }
}
于 2013-12-02T19:06:26.640 に答える
3

補間用に別の変数を維持し、フレームごとに更新する必要があります。そうしないと、Time.deltaTime * rotationSpeed が 0 ~ 1 の範囲を超えて永遠に上昇し続けます。

private float _RawLerp;
private float _Lerp;
public float _Speed;
public transform _Source;
public transform _Target;

private transform _TransformCache; // the transform for my game object, set in the Awake method

public void Update()
{
    _RawLerp += Time.deltaTime * _Speed;
     _Lerp = Mathf.Min(_RawLerp, 1); 
   _TransformCache.rotation = Quaternion.Slerp(
         _Source.TargetRotation(),
         _Target.TargetRotation(), 
         _Lerp);
}
于 2013-12-02T17:59:52.213 に答える