C# 使球跳跃

C# 使球跳跃,c#,unity3d,C#,Unity3d,我正在尝试制作一个脚本,在那里我可以水平和垂直移动一个球。我设法让它工作起来了 但现在我想让我的球“跳跃”。我以下面的脚本结束,但现在我的球像火箭xD一样发射了 有人能帮我吗 using UnityEngine; using System.Collections; public class PlayerController : MonoBehaviour { public float speed; public float jumpSpeed; public GUIT

我正在尝试制作一个脚本,在那里我可以水平和垂直移动一个球。我设法让它工作起来了

但现在我想让我的球“跳跃”。我以下面的脚本结束,但现在我的球像火箭xD一样发射了

有人能帮我吗

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    public float speed;
    public float jumpSpeed;
    public GUIText countText;
    public GUIText winText;
    private int count;

    void Start()
    {
        count = 0;
        SetCountText();
        winText.text = " "; 
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
        Vector3 jump = new Vector3 (0, jumpSpeed, 0);

        GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);

        if (Input.GetButtonDown ("Jump"));
        GetComponent<Rigidbody>().AddForce (jump * jumpSpeed * Time.deltaTime);

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "PickUp") {
            other.gameObject.SetActive(false);
            count = count +1;
            SetCountText();
        }
    }

    void SetCountText()
    {
        countText.text = "Count: " + count.ToString();
        if (count >= 10) 
        {
            winText.text = "YOU WIN!";
        }
    }
}
使用UnityEngine;
使用系统集合;
公共类玩家控制器:单行为
{
公众浮标速度;
公众浮标跳跃速度;
公共文本;
公共吉他文本;
私人整数计数;
void Start()
{
计数=0;
SetCountText();
winText.text=“”;
}
void FixedUpdate()
{
float moveHorizontal=Input.GetAxis(“水平”);
float moveVertical=Input.GetAxis(“垂直”);
Vector3移动=新的Vector3(水平移动、0、垂直移动);
矢量3跳跃=新矢量3(0,跳跃速度,0);
GetComponent().AddForce(移动*速度*时间.deltaTime);
if(Input.GetButtonDown(“跳转”);
GetComponent().AddForce(跳跃*跳跃速度*时间.deltaTime);
}
无效对撞机(对撞机其他)
{
如果(other.gameObject.tag==“拾取”){
other.gameObject.SetActive(false);
计数=计数+1;
SetCountText();
}
}
void SetCountText()
{
countText.text=“Count:+Count.ToString();
如果(计数>=10)
{
winText.text=“你赢了!”;
}
}
}

跳跃不适用于在对象上添加连续力。当第一次按下跳转按钮时,必须对对象施加一次脉冲。该脉冲也不包括时间因素,因为它只应用一次。所以你会得到这样的结果:

bool jumping;

if (Input.GetButtonDown ("Jump") && !this.jumping);
{
    GetComponent<Rigidbody>().AddForce (jumpForce * new Vector3(0,1,0));
    this.jumping = true;
}
bool跳跃;
if(Input.GetButtonDown(“Jump”)&&!this.jumping);
{
GetComponent().AddForce(jumpForce*新向量3(0,1,0));
这是真的;
}
还要注意,在您的示例中,向上单位向量乘以跳跃速度两倍。一次在
jump
向量初始化中,然后一次在
AddForce
方法中

当然,您还必须确保应用重力将对象拉回地面(如果对象撞到地面,则重置跳跃布尔)


一般来说,根据您正在进行的游戏类型,只需自己设置对象的速度,而不使用Unity physics引擎进行简单的移动,就更容易了。

函数
FixedUpdate
中的代码有错误:

if (Input.GetButtonDown ("Jump"));
通过这种方式,您在每一帧对对象施加一个力,因为分号将下面的行排除在条件之外。只要在刚体组件上启用了UseGravity,在跳转的情况下删除分号,您将有一个正确的if实现

if (Input.GetButtonDown ("Jump"))
    GetComponent<Rigidbody>().AddForce (jump * jumpSpeed * Time.deltaTime);
if(Input.GetButtonDown(“跳转”))
GetComponent().AddForce(跳跃*跳跃速度*时间.deltaTime);

希望有帮助。

谢谢大家,这是一个很大的帮助。我现在有了一个跳跃的角色。一个只有在脚踏实地时才能跳跃的角色

public bool IsGrounded;

    void OnCollisionStay (Collision collisionInfo)
{
    IsGrounded = true;  
}

void OnCollisionExit (Collision collisionInfo)
{
    IsGrounded = false;
}


if (Input.GetButtonDown ("Jump") && IsGrounded)
    {
        GetComponent<Rigidbody>().velocity = new Vector3(0, 10, 0);
    }
公共场所接地;
void OnCollisionStay(碰撞信息)
{
IsGrounded=true;
}
void OnCollisionExit(碰撞碰撞信息)
{
isfounded=false;
}
if(Input.GetButtonDown(“跳转”)和&isground)
{
GetComponent().velocity=新向量3(0,10,0);
}

我似乎记得读过这样一篇文章:对于跳跃,你最好只设定速度,而不是试图给角色施加一种冲动。谢谢,这非常有用。我决定选择速度部分。