[英]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.