统一无僵尸跳跃 [英] Jumping without rigidbody in unity
问题描述
我试图在我们的控制脚本的控件中添加跳转,但是它仍然不起作用,并且我感到很沮丧,因为我做了很多尝试来使它起作用.我不使用刚体,而是想使用基于脚本的物理学,但后来使用光线投射(以检测地面).它是3D,但具有固定的角度,因为角色是精灵(我们正在考虑尝试类似于不要挨饿",纸马里奥"等样式).我现在要输入的内容是,当角色静止不动时向上跳,然后再进行跳跃,这也使角色移动时角色可以走一段距离.好吧,你知道了.为此,我首先需要完全跳下来,还需要重力来将角色放回原处,同时还要考虑角色可以降落在与起始地面不同高度的地面上. 现在发生的是,角色跳了一点点,甚至没有跳,然后无休止地摔倒-或再次按下跳动时无休止地爬升.那么,我该如何解决呢?
I'm trying to add jumping to the controls of our control script but it just doesn't work yet and I'm getting kind of frustrated because I tried a lot to make it work. I don't use rigidbodies and rather want to use script based physics and - but later - raycasts (to detect ground). It's 3D but with a fixed perspective because the characters are sprites (we are thinking about trying a style similar to Don't Starve, Paper Mario etc.). And the thing I want to put in for now is to jump upwards when the character stands still and later on jumps that also let the character travel a bit of distance when the character moves. Well, you get the picture. For that I first need to get jumping working at all - and also gravity to put the character back down while also considering that the character could land on a ground that has a different height than the starting ground. What happens now is that the character jumps just a tiny little bit, like not even a hop, and then falls down endlessly - or goes up endlessly when jump is pressed again. So, how do I fix that?
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;
}
}
}
推荐答案
如果我是你,我想我会把整个东西变成一个字符控制器,因为这简化了#### ton:P
If i were you, i think i wouldve changed the whole thing into a character controller, as this simplifies the process, a ####ton :P
如果确定要使用CC.这是我通常使用的解决方案:
If you figure out you do want to use the CC. This is the solution i usually use:
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类型变量,而speed和jumpSpeed是用于修改速度的公共浮点值.
moveDirection is a Vector3 Type variable, and speed and jumpSpeed are public float values used to modify the speed.
请注意:字符控制器,让您对自己的物理进行编程.
Please NOTE: Character Controllers, let you program your own Physics.
这篇关于统一无僵尸跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!