0

オブジェクトを斜めに動かすコントローラーがあります。左矢印はプレーヤーを前方左に 45 度移動し、右矢印は同じように右に移動する必要があります。プレーヤーを現在の位置に相対的に移動したいと思います。現在、ポイント(0,0,0)に対して相対的に移動しています。

私のコード:

public class JollyJumper : MonoBehaviour {

protected CharacterController control;
    public float fTime = 1.5f;      // Hop time
    public float fRange = 5.0f;     // Max dist from origin
    public float fHopHeight = 2.0f; // Height of hop

    private Vector3 v3Dest;
    private Vector3 v3Last;
    private float fTimer = 0.0f;
    private bool moving = false;
    private int number= 0;
    private Vector2 direction;

    public virtual void Start () {

        control = GetComponent<CharacterController>();

        if(!control){

            Debug.LogError("No Character Controller");
            enabled=false;

        }

    }


    void Update () {

       if (fTimer >= fTime&& moving) {
        var playerObject = GameObject.Find("Player");
        v3Last = playerObject.transform.position;
        Debug.Log(v3Last);
        v3Dest = direction *fRange;
        //v3Dest = newVector* fRange + v3Last;

         v3Dest.z = v3Dest.y;
         v3Dest.y = 0.0f;
         fTimer = 0.0f;
        moving = false;
       }

        if(Input.GetKeyDown(KeyCode.LeftArrow)){
            moving = true;
            direction = new Vector2(1.0f, 1.0f);
            number++;
        }

        if(Input.GetKeyDown(KeyCode.RightArrow)){
            moving = true;
            direction = new Vector2(-1.0f, 1.0f);
            number++;
        }
        if(moving){
       Vector3 v3T = Vector3.Lerp (v3Last, v3Dest, fTimer / fTime);
       v3T.y = Mathf.Sin (fTimer/fTime * Mathf.PI) * fHopHeight;
       control.transform.position = v3T;
       fTimer += Time.deltaTime;
        }
    }
}

どうすればこれを解決できますか? 何か案は?どうもありがとう!

4

1 に答える 1

6

簡単に言えば、ジャンプ先の 2 つの場所 (ポイント (1, 1) と (-1, 1)) をハードコーディングしたということです。ジャンプを開始するたびに、新しいベクターを作成する必要があります。それぞれ交換

direction = new Vector2(1.0f, 1.0f);

この行で:

v3Dest = transform.position + new Vector3(1.0f, 0, 1) * fRange;

そしてそれはうまくいくはずです。

私がそれに取り組んでいる間、指摘したいことが他にもいくつかあります。

  • 各ジャンプの後、多くの浮動小数点エラーがあります。フラグを先に切り替えているため、コードでv3T決して等しくないことに注意してくださいv3Dest(実際に目的地に到達することはありません) 。ジャンプが終わったときのmoving位置を明示的に設定する必要があります。v3Dest
  • フレームごとにジャンプタイマーなどをチェックしています。よりエレガントな解決策は、コルーチンを開始することです。
  • ジャンプ カーブとして正弦波を使用すると問題ないように見えますが、放物線を使用する方が概念的にはより正確です。
  • 現在、空中で次のジャンプを開始することが可能です(意図しているかどうかはわかりません)

これらの問題を回避するために使用できるコードを次に示します。

using System.Collections;
using UnityEngine;

public class Jumper : MonoBehaviour
{
#region Set in editor;

public float jumpDuration = 0.5f;
public float jumpDistance = 3;

#endregion Set in editor;

private bool jumping = false;
private float jumpStartVelocityY;

private void Start()
{
    // For a given distance and jump duration
    // there is only one possible movement curve.
    // We are executing Y axis movement separately,
    // so we need to know a starting velocity.
    jumpStartVelocityY = -jumpDuration * Physics.gravity.y / 2;
}

private void Update()
{
    if (jumping)
    {
        return;
    }
    else if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        // Warning: this will actually move jumpDistance forward
        // and jumpDistance to the side.
        // If you want to move jumpDistance diagonally, use:
        // Vector3 forwardAndLeft = (transform.forward - transform.right).normalized * jumpDistance;
        Vector3 forwardAndLeft = (transform.forward - transform.right) * jumpDistance;
        StartCoroutine(Jump(forwardAndLeft));
    }
    else if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        Vector3 forwardAndRight = (transform.forward + transform.right) * jumpDistance;
        StartCoroutine(Jump(forwardAndRight));
    }
}

private IEnumerator Jump(Vector3 direction)
{
    jumping = true;
    Vector3 startPoint = transform.position;
    Vector3 targetPoint = startPoint + direction;
    float time = 0;
    float jumpProgress = 0;
    float velocityY = jumpStartVelocityY;
    float height = startPoint.y;

    while (jumping)
    {
        jumpProgress = time / jumpDuration;

        if (jumpProgress > 1)
        {
            jumping = false;
            jumpProgress = 1;
        }

        Vector3 currentPos = Vector3.Lerp(startPoint, targetPoint, jumpProgress);
        currentPos.y = height;
        transform.position = currentPos;

        //Wait until next frame.
        yield return null;

        height += velocityY * Time.deltaTime;
        velocityY += Time.deltaTime * Physics.gravity.y;
        time += Time.deltaTime;
    }

    transform.position = targetPoint;
    yield break;
}

}

于 2013-06-13T15:01:17.700 に答える