- Home /
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.
}
}
}
Your answer
Follow this Question
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