【发布时间】:2015-04-16 05:37:23
【问题描述】:
我是 Unity 的新手(以及一般的游戏开发)。我遵循了很棒的简单教程Survival Shooter,我有一个问题:在本教程中,我们为刚体的角色添加了一个 Y 约束位置,并将拖动值和角度拖动值设置为无限。由于这些设置会阻止角色移动到 Y 轴,我们如何让角色跳跃?
如果有人可以帮我解决这个问题,请...
非常感谢!
【问题讨论】:
我是 Unity 的新手(以及一般的游戏开发)。我遵循了很棒的简单教程Survival Shooter,我有一个问题:在本教程中,我们为刚体的角色添加了一个 Y 约束位置,并将拖动值和角度拖动值设置为无限。由于这些设置会阻止角色移动到 Y 轴,我们如何让角色跳跃?
如果有人可以帮我解决这个问题,请...
非常感谢!
【问题讨论】:
为什么要在 Y 轴上添加约束?您可以将其移除,然后添加重力,让您的玩家粘在地面上。之后,只需施加一个力,或者只是以设定的速度向上移动,让玩家跳跃,然后等待重力将他拉下来。
【讨论】:
这就是我要跳的。
附:你需要移除向量上 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 的标签称为地。并且还请记住在您的播放器对象上分配其他值。
【讨论】: