问题描述
谁能和我分享一个脚本,我可以用它来跳过这个脚本的角色?我将不胜感激,我 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.
推荐答案
我建议从他们网站上的一些课程开始(http://unity3d.com/learn ),但要回答您的问题以下是一个可以工作的通用脚本.
I would recommend starting with some of the courses on their website (http://unity3d.com/learn ),but to answer your question the followingis 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;
我们需要确定它们是否在地面上.Bool(或布尔值)表示是我们是(真),或者不是我们不是(假).
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,mode 是施加力的方式(模式可以是 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 中跳转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!