0
我有这个C#脚本附加到我的主相机游戏对象,它也有一个胶囊collider属性。但是,它似乎没有做任何事情。我应该如何修改/添加以使相机“跳”并再次掉到地上?当按下按键(最好是空格)时,如何让游戏对象跳转?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour {
[HideInInspector] public bool jump = false;
public float jumpForce = 1000f;
public Transform groundCheck;
private bool grounded = false;
private Rigidbody rb;
// Use this for initialization
void Awake()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if (Input.GetButtonDown("Jump") && grounded)
{
jump = true;
}
}
void FixedUpdate()
{
if (jump)
{
rb.AddForce(new Vector2(0f, jumpForce));
jump = false;
}
}
}
此外,我希望有关键这是空格,如果可能的话,但无论关键作品或已经有没有好。我仍然在学习C#,所以如果解决方案很明显,请原谅我。
我知道你在你的问题中有一个代码,但我有一种感觉,你没有写这个问题的方式。我建议你学习Unity如何工作。只需按照他们的[项目教程](https://unity3d.com/learn/tutorials)中的任何一个,您就可以开始编写自己的代码。 – Programmer
至于改变应该按下的键,使用'KeyCode.Space'。您可以看到从[this](https://stackoverflow.com/a/44539475/3785314)文章跳转的示例。虽然,如果你不知道如何设置图层并将地面放在正确的图层中,它将不起作用。 – Programmer
@程序员我已将地面放在地面层。我在哪里可以使用KeyCode.Space?有没有地方可以设置使用哪个键? – TrumpetDude