C# 使用转换函数[Unity 2D]进行跳转时出现问题
Soo,我正试图编写一个简单的平台,作为课堂上的工作,我在跳跃方面遇到了问题,角色实际上是跳跃的,但它似乎是一个传送,因为它立即达到跳跃的峰值,然后逐渐下降,我想让它更平滑C# 使用转换函数[Unity 2D]进行跳转时出现问题,c#,unity3d,C#,Unity3d,Soo,我正试图编写一个简单的平台,作为课堂上的工作,我在跳跃方面遇到了问题,角色实际上是跳跃的,但它似乎是一个传送,因为它立即达到跳跃的峰值,然后逐渐下降,我想让它更平滑 using System.Collections; using System.Collections.Generic; using UnityEngine; public class playerMovement : MonoBehaviour { public float speed; public f
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
public float speed;
public float jumpForce;
public bool grounded;
public Transform groundCheck;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
float moveVertical = 0;
float moveHorizontal = Input.GetAxis("Horizontal") * speed;
moveHorizontal *= Time.deltaTime;
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if (grounded && Input.GetKeyDown("space"))
{
moveVertical = Input.GetAxis("Jump") * jumpForce;
moveVertical *= Time.deltaTime;
}
Vector2 move = new Vector2(moveHorizontal, moveVertical);
transform.Translate(move);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家运动:单一行为{
公众浮标速度;
公共安全部队;
公共场所;
公开审查;
私有刚体2d rb2d;
//用于初始化
无效开始(){
rb2d=GetComponent();
}
//每帧调用一次更新
无效更新(){
}
私有void FixedUpdate()
{
浮动垂直=0;
float moveHorizontal=Input.GetAxis(“水平”)*速度;
moveHorizontal*=Time.deltaTime;
grounded=Physics2D.Linecast(transform.position,groundCheck.position,1问题的答案如下,但是,对于未来与Unity相关的问题,请在Unity论坛上提问。随着更多人关注Unity本身,您将更快地得到更好的答案
要解决跳跃问题,请将垂直和水平运动分成两部分,并使用在Start()函数中指定的刚体来处理跳跃。使用ForceMode.pulse可获得瞬间爆发的速度/加速度
private void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal") * speed;
moveHorizontal *= Time.deltaTime;
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if (grounded && Input.GetKeyDown("space"))
{
rb2d.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
Vector2 move = new Vector2(moveHorizontal, 0);
transform.Translate(move);
}
private void FixedUpdate()
{
float moveHorizontal=Input.GetAxis(“水平”)*速度;
moveHorizontal*=Time.deltaTime;
grounded=Physics2D.Linecast(transform.position,groundCheck.position,1在跳跃时不设置位置,而是使用以下命令向角色的刚体添加垂直脉冲:
if (grounded && Input.GetKeyDown("space"))
{
moveVertical = Input.GetAxis("Jump") * jumpForce;
moveVertical *= Time.fixedDeltaTime;
rb2d.AddForce(moveVertical, ForceMode.Impulse);
}
Vector2 move = new Vector2(moveHorizontal, 0f);
这将允许Rigidbody2D
处理改变每帧上垂直速度的工作
此外,由于您在FixedUpdate
中进行所有这些计算,而不是Update
,因此使用Time.deltaTime
没有任何意义。您应该使用Time.fixeddeltime
。通过使用.Alr在刚体上添加向上的力,您将获得更好的结果已经解决了!谢谢,我太专注于尝试使用Translate函数了,我完全忘记了AddForce,它现在可以工作了。这应该使用时间。fixedDeltaTime
而不是时间。deltaTime
,因为它的计算应该独立于绘图帧率。