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

简短的回答是:您硬编码了两个要跳转到的位置:点 (1, 1) 和 (-1, 1)。每次开始跳跃时都应该创建新的 Vector。更换每个

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 回答