简体   繁体   English

如何在 Unity 3d 中跳转?

[英]How to jump in Unity 3d?

can anyone share with me a script that I could use for jumping of the character for this script?任何人都可以与我分享一个脚本,我可以用它来跳跃这个脚本的角色吗? I would greatly appreciate it, I'm 12 and just starting, it would help me to finish my project.我将不胜感激,我 12 岁,刚刚开始,它将帮助我完成我的项目。 Thank you in advance.先感谢您。

I would recommend starting with some of the courses on their website ( http://unity3d.com/learn ),but to answer your question the following is a general script that would work.我建议从他们网站上的一些课程开始( http://unity3d.com/learn ),但要回答您的问题,以下是一个可行的通用脚本。

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour {
    public Vector3 jump;
    public float jumpForce = 2.0f;

    public bool isGrounded;
    Rigidbody rb;
    void Start(){
        rb = GetComponent<Rigidbody>();
        jump = new Vector3(0.0f, 2.0f, 0.0f);
    }

    void OnCollisionStay(){
        isGrounded = true;
    }

    void Update(){
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded){

            rb.AddForce(jump * jumpForce, ForceMode.Impulse);
            isGrounded = false;
        }
    }
}

Lets break that down a bit:让我们分解一下:

[RequireComponent(typeof(Rigidbody))] 

Want to make sure you have a rigidbody first before we make any calculations.在我们进行任何计算之前,要确保您首先拥有一个刚体。

public Vector3 jump; 

Vector3 is a variable storing three axis values. Vector3 是一个存储三个轴值的变量。 Here we use it to determine where we're jumping.在这里,我们使用它来确定我们在哪里跳跃。

public bool isGrounded; 

We need to determine if they're on the ground.我们需要确定它们是否在地面上。 Bool (or boolean) for yes we are (true), or no we are not (false).布尔值(或布尔值)表示是我们是(真),或者不是我们不是(假)。

void OnCollisionStay(){
    isGrounded = true;
}

in Start() , we assign the variable rb (set from Rigidbody rb ) to the component attached to your GameObj and also we assign values to the Vector3 jump.Start()中,我们将变量 rb (从Rigidbody rb设置)分配给附加到您的 GameObj 的组件,并且我们为 Vector3 跳转分配值。

Then we Update() with this:然后我们用这个Update()

if(Input.GetKeyDown(KeyCode.Space) && isGrounded){     
    rb.AddForce(jump * jumpForce, ForceMode.Impulse);
    isGrounded = false;
}

means that if the player hits the Space button and at the same time, the GameObj is grounded, it will add a physic force to the rigidbody, using.意味着如果玩家按下 Space 按钮,同时 GameObj 接地,它将给刚体添加一个物理力,使用。

AddForce(Vector3 force, ForceMode mode)

where force is the Vector3 storing the movement info and mode is how the force will be applied (mode can be ForceMode.Force, ForceMode.Acceleration, ForceMode.Impulse or ForceMode.VelocityChange, see ForceMode for more).其中 force 是存储运动信息的 Vector3,mode 是施加力的方式(mode 可以是 ForceMode.Force、ForceMode.Acceleration、ForceMode.Impulse 或 ForceMode.VelocityChange,更多信息请参见 ForceMode)。

Lastly, google is your best friend.最后,谷歌是你最好的朋友。 Be sure exhaust your options in the future in order to get the fastest results!一定要在未来用尽你的选择,以获得最快的结果!

Answer is a simplified rewrite of this: https://answers.unity.com/questions/1020197/can-someone-help-me-make-a-simple-jump-script.html答案是对此的简化重写: https://answers.unity.com/questions/1020197/can-someone-help-me-make-a-simple-jump-script.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM