如何跳入Unity 3d? [英] How to jump in Unity 3d?

查看:118
本文介绍了如何跳入Unity 3d?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以和我分享一个脚本,我可以用它来跳过该脚本中的角色吗? 我将不胜感激,我12岁,刚刚开始,它将帮助我完成项目.预先谢谢您.

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. 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.

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;
        }
    }
}

让我们分解一下:

[RequireComponent(typeof(Rigidbody))] 

在进行任何计算之前,请先确保您具有刚体.

Want to make sure you have a rigidbody first before we make any calculations.

public Vector3 jump; 

Vector3是存储三个轴值的变量.在这里,我们用它来确定我们要跳到哪里.

Vector3 is a variable storing three axis values. Here we use it to determine where we're jumping.

public bool isGrounded; 

我们需要确定它们是否在地面上.布尔(或布尔),是的,我们是(true),否则,我们不是(false).

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;
}

Start()中,我们将变量rb(从Rigidbody rb设置)分配给与GameObj相连的组件,还为Vector3跳转分配值.

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.

然后我们Update()与此:

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

意思是,如果玩家按下"Space"按钮,同时GameObj接地,它将使用施加一个物理力给刚体.

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.

AddForce(Vector3 force, ForceMode mode)

其中force是存储运动信息的Vector3,模式是施加力的方式(模式可以是ForceMode.Force,ForceMode.Acceleration,ForceMode.Impulse或ForceMode.VelocityChange,有关更多信息,请参见ForceMode.)

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).

最后,谷歌是你最好的朋友.为了确保最快的结果,请确保将来用尽所有选择!

Lastly, google is your best friend. Be sure exhaust your options in the future in order to get the fastest results!

答案是对此内容的简化重写: https://answers.unity.com/questions/1020197/can-someone-help-me-make-a-simple-jump-script.html

Answer is a simplified rewrite of this: https://answers.unity.com/questions/1020197/can-someone-help-me-make-a-simple-jump-script.html

这篇关于如何跳入Unity 3d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆