我对 Unity(以及一般的游戏开发)完全陌生。我遵循了伟大的简单教程Survival Shooter并且我有一个问题:在本教程中,我们为刚体的角色添加了 Y 约束位置,并将拖动值和角度拖动值设置为无限。既然这些设置阻止了角色移动到 Y 轴,我们如何让角色跳跃?
如果有人可以帮我解决这个问题,请...
非常感谢!
我对 Unity(以及一般的游戏开发)完全陌生。我遵循了伟大的简单教程Survival Shooter并且我有一个问题:在本教程中,我们为刚体的角色添加了 Y 约束位置,并将拖动值和角度拖动值设置为无限。既然这些设置阻止了角色移动到 Y 轴,我们如何让角色跳跃?
如果有人可以帮我解决这个问题,请...
非常感谢!
为什么要在 Y 轴上添加约束?您可以将其移除,然后添加重力以使您的玩家粘在地面上。之后,只需施加一个力,或者只是以设定的速度向上移动,让玩家跳跃,然后等待重力将他拉下来。
这就是我要跳的。
PS你需要去掉向量上Y轴上的约束
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public Vector3 force;
public Rigidbody rb;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space) && transform.position.y == 0) //Enter your y axix where ground is located or try to learn a little more about raycasting ill just use 0 for an example)
{
rb.AddForce(force);//Makes you jump up when you hold the space button down line line 19 will do so that you only can jump when you are on the ground.
} if (Input.GetKeyUp(KeyCode.Space))
{
rb.AddForce(-force); //When you realase the force gets inverted and you come back to ground
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Just : MonoBehaviour {
public Vector3 force;
public Rigidbody rb;
bool isGrounded;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) //Rember to got to your "Ground" object and tag it as Ground else this would not work
{
rb.AddForce(force);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
isGrounded = false;
}
}
您需要为您的地面对象分配一个名为 Ground 的标签,您需要创建一个名为 Ground 的自己的标签,您单击对象并在检查器的左上方有标签,然后您只需制作一个名为 Ground 的新标签。并且还请记住在您的播放器对象上分配其他值。