没有物理的Unity瓷砖跳跃

问题描述 投票:0回答:0

我试图在不使用统一物理的情况下创建从一个瓷砖到另一个瓷砖的跳跃。现在我有一个工作完美的工作解决方案,但我想要一个我无法自己编码的改进。

在这两张照片中,我展示了我拥有的和我想要的。 enter image description here

我的目标是我的玩家能够在跳跃快完成时再次跳跃(我做到了但充满了错误),并且这第二个三分之一等连续跳跃与我的 archeigh 变量一样高。

这是我的代码,希望你们能理解我的问题。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private PlayerControls controls;

    Vector3 _startPosition;
    Vector3 lastPosition;
    Quaternion targetRotation;
    bool jumping = false;
    public AudioSource audioListener;
    public AudioClip JumpSound;
    public float rayLenght = 1.4f;
    public LayerMask groundLayer;

    float parabola = 0f;
    float _stepScale;
    float _progress;
    public float speed;
    public float arcHeight;
    Vector3 targetDestination;
    private float targetArcHeight;

    private bool ableToJump = true;

    private void Awake()
    {
        controls = new PlayerControls();
    }

    private void OnEnable()
    {
        controls.Enable();
    }

    private void OnDisable()
    {
        controls.Disable();
    }
    // Start is called before the first frame update
    void Start()
    {
        controls.Main.Movement.performed += ctx => Move(ctx.ReadValue<Vector2>());
        _startPosition = transform.position;
        targetArcHeight = arcHeight;
        lastPosition = transform.position;
    }

    private void Move(Vector2 direction)
    {
        if (ableToJump)
        {
            _progress = 0f;
           // parabola = 0f;
            ableToJump = false;
            _startPosition = transform.position;

            CalculateArcHeight();

            Vector3 movement = new Vector3(direction.x, 0, direction.y);

            targetDestination = lastPosition + movement;
            lastPosition = targetDestination;
            float distance = Vector3.Distance(_startPosition, targetDestination);
            _stepScale = speed / distance;

            audioListener.PlayOneShot(JumpSound);
            targetRotation = Quaternion.LookRotation(targetDestination - _startPosition);
            jumping = true;

            
        }


    }
    // Update is called once per frame
    void Update()
    {
        if (jumping)
        {
            
            // Increment our progress from 0 at the start, to 1 when we arrive.
            _progress = Mathf.Min(_progress + Time.deltaTime * _stepScale, 1.0f);

            // Turn this 0-1 value into a parabola that goes from 0 to 1, then back to 0.
            parabola = 1.0f - 4.0f * (_progress - 0.5f) * (_progress - 0.5f);

            // Travel in a straight line from our start position to the target.        
            Vector3 nextPos = Vector3.Lerp(_startPosition, targetDestination, _progress);

            
            // Then add a vertical arc in excess of this.
            nextPos.y += parabola * targetArcHeight;

            transform.position = nextPos;

            // Arrived
            if (_progress >= 0.6f)
            {
                ableToJump = true;
            }

        }
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 10f * Time.deltaTime);

    }

    private void CalculateArcHeight()
    {
        float currentHeight = transform.position.y;
        targetArcHeight = targetArcHeight - currentHeight;

    }

}

附言我的旋转有时也会卡在奇怪的 x 旋转上。 提前致谢

我做到了二段跳,但无法计算出新高度为 1(我的足弓高度)。

c# unity3d
© www.soinside.com 2019 - 2024. All rights reserved.