- Home /
What is wrong with this rigidbody?
So im just trying to make a ball move with constant velocity in the x direction, the thing is when i write a certain velocity it will work normally:
public Rigidbody thisRigidbody;
void Start () {
thisRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate () {
thisRigidbody.velocity = new Vector3(5, thisRigidbody.velocity.y, thisRigidbody.velocity.z);
}
But i want to modify that velocity through a float variable (playerSpeed):
public float playerSpeed = 0;
public Rigidbody thisRigidbody;
void Start () {
thisRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate () {
thisRigidbody.velocity = new Vector3(playerSpeed, thisRigidbody.velocity.y, thisRigidbody.velocity.z);
}
the weird thing is even when i use playerSpeed in 0 like in the code, the ball will move in x anyways in a certain velocity, and when i modify the playerSpeed to some other value, it still moves in that velocity, idk if i'm missing something help guys
Answer by bhavinbhai2707 · Apr 10, 2018 at 09:40 PM
Well the only possible mistake I can guess is that you are changing value of playerSpeed in the script itself and not inspector. Once you set the value of a public variable and set it to something, and then if u change the value of that variable in inspector then the Compiler will always take the value which is set in the inspector and will never look back to value which you set in script even if you change it. So make sure you are changing value of playerSpeed in the inspector.
Answer by Ajesus- · Apr 11, 2018 at 03:18 PM
thank you man, i had no idea it was totally necessary to check the public variables in the inspector, for some reason playerSpeed was set to 10 in the inspector by default
Your answer
Follow this Question
Related Questions
Rigidbody.velocity giving weird result 0 Answers
rigidbody.velocity.normalized application 1 Answer
Strange/Buggy code behavier 1 Answer
Space Shooter tutorial shot velocity issues 0 Answers
Distribute terrain in zones 3 Answers