Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Bigwater97 · Jun 09, 2014 at 03:56 PM · c#movementphysicsrigidbody

Unity Physics On Input Issue?

I am making a game where the player controls how far a spring moves back by varying the amount of time the space bar is held down. It is using a Input.GetKey statement to detect when the Spacebar is down and a little later in the script is a GetKeyUp reference that obviously tells the engine to move the spring back forward at speed relevent to the CurrentPower float variable.

When I hold the spring in only occasionally when I let the spacebar out the spring does not bounce back forward and I am forced to press the space bar again to make it work.

Any help that you may have to offer would be greatly appreciated! Thanks

Script

 using UnityEngine;
 using System.Collections;
 
 public class SpringMechanics : MonoBehaviour {
 
     public GameObject Spring; //Spring Object
     public float springRetractSpeed; //Units Per Second Spring Will move backwards when mouse button is held down.
     public float PowerPerSecond; //Units Per Second the spring will move back into out position at when torque is released by user
     public GameObject GreenPowerBar; //Green Power Bar Object
     private bool SpringBeingCompressed; //Determines Whether Spring Is Being Moved Backwards Or Not
     private bool PlayerTurnOver; //Determines Whether the player has used there one turn
     private int CurrentPower; //Amount Of Power Spring Has Accumulated
     public GameObject Player; //GameObject For The Player
 
     void FixedUpdate()
     {
         if (Spring.transform.position.x <= -3 && Spring.transform.position.x > -3.19f && Input.GetKey(KeyCode.Space)) //If Spring is out but not to far in & mouse button is being held
         {
             Spring.transform.position = new Vector3 (transform.position.x - springRetractSpeed * Time.deltaTime,transform.position.y,transform.position.z); //Spring Moves backwards at a speed determined by the value of the RetractSpeed variable
             CurrentPower++;             //Pluses 1 to the current power so that the spring will have varying amounts of momentum depending on the time it has being compressed.
 
         
         }
         if (Input.GetKeyUp(KeyCode.Space))
         {
                 rigidbody.velocity = new Vector3 (CurrentPower/10, 0, 0);
         }
     }
 
     void Update ()
     {
 
 
         if (transform.position.x >= -2.6f)                                                              // 2.6 is the maximum that the spring can come out, this value can be adjusted however the end of the spring should not show.
         {
 
             rigidbody.velocity = Vector3.zero;                                                         // This Line changes the springs velocity to 0 to stop it from moving foreward past the maximum allowed distance as defined above.
         
             rigidbody.AddForce (0, 0, 0);                                                            // This Line adds a force to the springs rigid body to cancel out any remaining forces applied in the FixedUpdate function as they would cause the spring to go foreward past its maximum limit teleport back over and over again.
 
             transform.position = new Vector3 (-2.6f, transform.position.y, transform.position.z);     // This Line Sets the transform.position.x of The Spring to its out position incase it was slightly off due to a drop or rise in the framerate of the game.
 
         }
     }
 }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

RigidBody immediately stops after AddForce 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Force to Velocity scaling? 2 Answers

Interpolate problem? Jump isn't smooth... 1 Answer

Sometimes the ball isn't moving 0 Answers


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