How do I make my "PlayerMovement" script and "Use Gravity" checkbox (rigidbody) enable when I click SPACEBAR in game?
Hey Unity Community! 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. 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 click SPACEBAR in order for the ball's "PlayerMov" script and "Use Gravity" checkbox (in Rigidbody) to enable. So instead of having the ball immediately move after it has respawned, it will now wait for the human player to click SPACEBAR before starting.
The script you can see below is my "EnableMovement" script where I attempted to fix this issue. But something's wrong, and I'm not quite sure what it is.
Any help would be greatly appreciated!
Thanks in advance, E.J (Fancy)
Answer by luckymonk3 · Jul 12, 2018 at 07:44 PM
Congrats on your first game! As of writing this I am assuming a little background in C# and/or javascript. Both of the scripts below are meant to be attached to the ball. Both of these script are also in javascript. So there are a couple of ways to solve this but Unity doesn't like people using && nor does it give the ability to check for Key-down and a collision at the same time (albeit easily). Now this script isn't really efficient but it gets the job done, and I don't think efficiency is really a problem in a roller-ball game. You mention that you want to respawn, freeze and when someone presses space it will start moving again. Instead of disabling the player movement script and the rigidbody gravity, we can simply use the rigidbodys kinematic function so that it will not move at all. So for the first script we basically just get the rigidbody component on the ball, enable rag-doll (disable kinematic) at start, setup functions for when the ball is or isn't rag-dolled, and say what happens when you hit the respawn collider (teleport back to beginning and disable rag-doll). The 'Check Cube' is for respawning which comes into play in the 2nd script This is the 1st script: #pragma strict var rb : Rigidbody; var Respawner : GameObject; var CheckCube : GameObject; function Start() { rb = gameObject.GetComponent(Rigidbody); EnableRagdoll(); } function OnCollisionEnter (col : Collision) { if(col.gameObject.name == "Respawner") { DisableRagdoll(); transform.position = new Vector3(0.0f, 2.0f, 0.0f); } } function DisableRagdoll() { Debug.Log ("Ragdoll Disabled"); rb.isKinematic = true; } function EnableRagdoll() { Debug.Log ("Ragdoll Enabled"); rb.isKinematic = false; }
or Now for the 2nd script we are checking if the ball has been respawned and if it has it will stay frozen until the player hits the space bar. For the 'Check Cube' I have it set as a trigger because it wont do anything other than support respawning the ball. The 'Check Cube' needs to placed where the cube respawns (same as the vector3 coordinates in the first script). This is the 2nd Script #pragma strict var rb : Rigidbody; var CheckCube : GameObject; function Start() { rb = gameObject.GetComponent(Rigidbody); } function OnTriggerStay () { if (Input.GetKey(KeyCode.Space)) rb.isKinematic = false; }
or
Hope this solved the problem.
Cheers!