- Home /
Why is velocity checking intensive?
I wanted to add a motion limiter so I change this code to he next. The game was then unplayable.
public class Movement : MonoBehaviour {
Rigidbody rb;
public float speed;
void Start(){
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate(){ //Physics Update
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 move = new Vector3 (moveHorizontal, 0, moveVertical);
rb.AddForce (move * Time.deltaTime * speed);
}
}
public class Movement : MonoBehaviour {
Rigidbody rb;
public float speed;
private Vector3 movement;
void Start(){
rb = GetComponent<Rigidbody> ();
movement = rb.velocity;
}
void LateUpdate()
{
movement = rb.velocity;
}
void FixedUpdate(){ //Physics Update
if (movement.y == 0) {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 move = new Vector3 (moveHorizontal, 0, moveVertical);
rb.AddForce (move * Time.deltaTime * speed);
}
}
}
This question is not phrased very well. The words "intensive" and "unplayable" are not descriptive enough to tell us what is happening.
If I had to guess, I'd say your problem is checking for equality on a velocity vector component. In the world of floating point math and dynamics simulations, you should not expect a vector component to be equal to some number. Check for approximation ins$$anonymous$$d.
http://docs.unity3d.com/ScriptReference/$$anonymous$$athf.Approximately.html
if ( $$anonymous$$athf.Approximately( movement.y, 0 ) ) {}
In my experience, $$anonymous$$athf.Approximately is a bit too strict on what it allows as "approximately" the same value.
What you're checking is if you're falling. Set some value as the $$anonymous$$imum velocity at which you consider that you're falling, and check if you're above that:
float $$anonymous$$imumFallingVelocity = -.1f;
....
void FixedUpdate(){ //Physics Update
if (movement.y > $$anonymous$$imumFallingVelocity)
....
}
Your answer
Follow this Question
Related Questions
Rigidbody Enemy and Collisions 2 Answers
2D metroid boost ball 0 Answers
Problem with making an object bounce. 2 Answers
Problem reading/getting from rb.velocity value? 1 Answer
different beetween transform.translat and rigidbody.addforce? 1 Answer