rigidbody.velocity.x doesn't work :(
The bools don't switch to true.. even if the RB is there. I wanted to define its position. Is this a right function to define it? Ty
private float rightmax = 1.94f;
private float leftmax = -1.96f;
private bool rightmaxconfirmed = false;
private bool leftmaxconfirmed = false;
private Rigidbody2D myRB;
private void Start()
{
myRB = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if(myRB.velocity.x >= rightmax)
{
rightmaxconfirmed = true;
}
else if(myRB.velocity.x <= leftmax)
{
leftmaxconfirmed = true;
}
}
Answer by Bunny83 · Nov 04, 2017 at 12:39 PM
Your question is more than confusing. You said:
I wanted to define its position.
However velocity is the rigidbodies velocity (speed), not it's position. Also you don't "define" or set anything here but you actually check if the speed in x direction is greater / smaller than the limits you set in your two variables.
So if you actually wanted to check the position you should do:
if(myRB.position.x >= rightmax)
The next thing you should keep in mind is when one of your if statements becomes true you set the corresponding boolean variable to true. Though the variables are never set back to false. So when the if statement is true for the first time your bool variable will stay true.
It's impossible to deduce what your script is supposed to do from the given description and code.
Answer by chiragjksol · Nov 04, 2017 at 11:45 AM
may be condition is not satisfy. Try to Debug your myRB.velocity.x and check value of it.
even i try like this way (Example):
Vector3 v3 = rigidbody.velocity;
v3.x = 1.0;
v3.z = 0.0;
rigidbody.velocity = v3;
thanks for a comment :) but im new in this and that guy under your comment realized what my question mean. But thx...