Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by FancySmash · Jul 11, 2018 at 06:40 PM · scripting problemmovementrigidbodygravitygetkey

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)

alt text

enablemovementscript.png (14.0 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoDumbQuestion · Jul 12, 2018 at 01:44 AM 0
Share

Input gotta be called in update()

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 alt text 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 alt text
Hope this solved the problem.
Cheers!


623f1982256af957980e0dac01dd49e6.png (30.3 kB)
fe79d311c9a178dbcf3b13795e8eca90.png (13.4 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

267 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The player falls down a ladder 0 Answers

No Gravity Jump 0 Answers

Issue with Rigidbody movement one step at a time and gravity 0 Answers

how to dostraight ball throwing objects from returning? 0 Answers

Why does the player not move around planet with gravity ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges