简体   繁体   English

C#Unity-当我在游戏中单击空格键时,如何启用“ PlayerMovement”脚本和“使用重力”复选框(刚体)?

[英]C# Unity - How do I make my “PlayerMovement” script and “Use Gravity” checkbox (rigidbody) enable when I click SPACEBAR in game?

I started making my first game a week ago and I've run into some problems with the coding. 我一周前开始制作第一款游戏,但在编码方面遇到了一些问题。 Basically, I have made a game where you play as a Ball. 基本上,我制作了一款您可以玩球的游戏。 The goal is to dodge the obstacles and reach the end of the level. 目标是躲避障碍并达到关卡的尽头。

I've made a respawner that puts the ball at the very beginning of the map whenever you die. 我制作了一个重生器,每当您死亡时,它都会将球放在地图的开始处。 My problem is that as soon as the ball has respawned it starts moving again. 我的问题是,一旦球重新产生,球就会再次开始运动。

What I have done so far to prevent the Ball from moving at the start is that I have DISABLED the Ball's PlayerMov script (player movement) and the checkbox "Use Gravity" in the Rigidbody component. 到目前为止,我为防止Ball在开始时移动而所做的事情是,我已禁用Ball的PlayerMov脚本(玩家移动)以及Rigidbody组件中的“使用重力”复选框。 So now my Ball is stuck at the start position unable to move. 所以现在我的球卡在了无法移动的起始位置。

So what do I need help with? 那我需要什么帮助呢? I want the human player to press the spacebar in order for the ball's PlayerMov script and "Use Gravity" checkbox (in Rigidbody) to enable. 我希望人类玩家按下空格键以使球的PlayerMov脚本和“使用重力”复选框(在Rigidbody中)启用。 So instead of having the ball immediately move after it has respawned, it will now wait for the human player to press the spacebar before starting. 因此,它不再是在重生之后立即移动球,而是将等待人类玩家在开始之前按下空格键。

The script you can see below is my EnableMovement script where I attempted to fix this issue. 您在下面看到的脚本是我的EnableMovement脚本,在该脚本中我试图解决此问题。 But something's wrong, and I'm not quite sure what it is. 但是出了点问题,我不太确定这是什么。

代码错误

You have to place it in update() method, so, the script will check every frames if the spacebar was pressed. 您必须将其放置在update()方法中,因此,如果按下空格键,脚本将检查每帧。

void Update()
{
    if (Input.GetKey(KeyCode.Space)) //notice the removing of the ;
    {
        GetComponent<PlayerMov>().enabled = true;
    }
}

There was a mistake in your code, when typing this : 输入此代码时,您的代码有误:

if (Input.GetKey(KeyCode.Space)); //notice the ;
{
    GetComponent<PlayerMov>().enabled = true;
}

This is the same thing than : 这与以下内容相同:

if (Input.GetKey(KeyCode.Space))
    ; //Do nothing
//there is a scope that will be executed
{
    GetComponent<PlayerMov>().enabled = true;
}

and this is exactly the same than this : 和这完全一样:

if (Input.GetKey(KeyCode.Space))
{
    ; //Do nothing
}
//there is a scope that will be executed
{
    GetComponent<PlayerMov>().enabled = true;
}

Edit: My mistake, the property you want to change is not enabled for a Rigidbody . 编辑:我的错误,您要更改的属性未enabled Rigidbody It is IsKinematic . 这是IsKinematic Also you probably want to disable collisions too. 另外,您可能也想禁用碰撞。 I've edited my code below. 我在下面编辑了我的代码。

A simple solution could be to have the physics disabled when the game is run, and enable it when the player presses space. 一种简单的解决方案是在运行游戏时禁用物理功能,并在玩家按下空格时启用物理功能。 Setting the Rigidbody.IsKinematic property to true means the GameObject won't be affected by physics. Rigidbody.IsKinematic属性设置为true意味着GameObject将不受物理影响。 That way you don't need to touch the gravity settings or disable the script. 这样,您无需触摸重力设置或禁用脚本。

Rigidbody _rigidbody;    

void Start() // or could go in the Awake() method depending on your game
{
    _rigidbody = GetComponent<Rigidbody>();
    TogglePhysics(false);
}

void Update()
{
    if (Input.GetKey(KeyCode.Space))
    {
        TogglePhysics(true);
    }
}

void TogglePhysics(bool isEnabled)
{
    // IsKinematic needs to be true to disable physics. See the documentation for IsKinematic.
    _rigidbody.IsKinematic = !isEnabled; 
    _rigidBody.detectCollisions = isEnabled;
}

// When respawning, call TogglePhysics(false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Unity C#中仅使用脚本中的游戏对象启用零件/组件 - How do I enable Parts/Components in Unity C# with only a game object in the script 如何在统一的脚本中启用/禁用重力? - How do I enable/disable gravity in a script in unity? 如何将重力体固定在统一脚本C#代码上? - How do i fix the gravity body on the unity script C# code? 如何在 Unity 的第一人称射击游戏中进行刚体运动? - How do I make Rigidbody movements in a first person shooter in Unity? 如何根据运动方向旋转带有 RigidBody 的游戏对象? 团结 | 3D | C# - How do I rotate a GameObject with a RigidBody based on it's movement direction? Unity | 3D | C# 如何使用C#为在Unity运行时创建的游戏对象分配一个on click侦听器? - How do I assign an on click listener to a game object that is created at runtime in Unity using C#? 如何在FixedUpdate中移动刚体,但我的角色仍然受到重力影响? - How do I move the Rigidbody in FixedUpdate, but still have my character be affected by gravity? 如何通过isKinematic检查使具有刚体的播放器移动? - How do I make my player with a Rigidbody move with isKinematic checked? 在 Unity 中与游戏对象发生碰撞时,如何让我的玩家加速? - How do I make my player speed up when collide with a game object in Unity? 如何使克隆的游戏对象成为全局对象,以便可以在Unity功能之外使用它们? - How do I make my cloned game objects global, so I can use them outside of the function in Unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM