没有刚体的团结跳跃

时间:2016-12-23 22:06:08

标签: c# unity3d

我试图将跳跃添加到我们的控制脚本的控件中,但它还没有工作,我感到很沮丧,因为我尝试了很多工作。我不使用刚体,而是希望使用基于脚本的物理,而后来使用光线投影(以检测地面)。这是3D,但具有固定的视角,因为角色是精灵(我们正在考虑尝试类似于唐·达夫,纸片马里奥等的风格)。而我现在想要放置的是当角色静止时向上跳跃,然后跳跃,当角色移动时角色也会移动一段距离。好吧,你明白了。为此我首先需要跳跃工作 - 并且重力也要将角色放回原位,同时还要考虑角色可以降落在高度与起始地不同的地面上。 现在发生的事情是,角色只是跳了一点点,甚至不跳跃,然后无休止地跌落 - 或者当再次按下跳跃时无休止地上升。那么,我该如何解决这个问题?

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

public class Controls3D : MonoBehaviour
{

    public float player3DSpeed = 5f;
    private bool IsGrounded = true;
    private const float groundY = (float) 0.4;
    private Vector3 Velocity = Vector3.zero;
    public Vector3 gravity = new Vector3(0, -10, 0);

    void Start()
    {
    }

    void Update()
    {
        if (IsGrounded)
        {
            if (Input.GetButtonDown("Jump"))
            {
                IsGrounded = false;
                Velocity = new Vector3(Input.GetAxis("Horizontal"), 20, Input.GetAxis("Vertical"));
            }
            Velocity.x = Input.GetAxis("Horizontal");
            Velocity.z = Input.GetAxis("Vertical");
            if (Velocity.sqrMagnitude > 1)
                Velocity.Normalize();
            transform.position += Velocity * player3DSpeed * Time.deltaTime;
        }
        else
        {
            Velocity += gravity * Time.deltaTime;
            Vector3 position = transform.position + Velocity * Time.deltaTime;
            if (position.y < groundY)
            {
                position.y = groundY;
                IsGrounded = true;
            }
            transform.position = position;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果我是你,我想我会把整个事情变成一个角色控制器,因为这简化了过程,#### ton:P

如果你弄清楚你确实想使用CC。这是我经常使用的解决方案:

CharacterController controller = GetComponent<CharacterController>();
    // is the controller on the ground?
    if (controller.isGrounded)
    {
        //Feed moveDirection with input.
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);



        //Multiply it by speed.
        moveDirection *= speed;
        //Jumping
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;

    }
    //Applying gravity to the controller
    moveDirection.y -= gravity * Time.deltaTime;
    //Making the character move
    controller.Move(moveDirection * Time.deltaTime);

moveDirection是一个Vector3 Type变量,speed和jumpSpeed是用于修改速度的公共浮点值。

请注意:角色控制器,让您自己编程物理。

相关问题