Since this wasn't clear enough, I've removed even more code. It kind of defeats the purpose I was aiming for, but I guess getting any answer relevant to the problem is better than none.
What I want: A cube to leave the ground with N velocity in the negative x direction, then gravity pulls it back down.
THE SETUP:
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update() {
//Jump
if (Input.GetKeyDown("space"))
{
jump = true;
}
}
TEST 1 (I really thought this should work as it was the solution I saw in so many google results)
void FixedUpdate()
{
if (jump)
{
rb.AddForce(new Vector3(0, 0, -5000f));
jump = false;
}
}
TEST 2 (Added ForceMode.VelocityChange)
void FixedUpdate()
{
if (jump)
{
rb.AddForce(new Vector3(0, 0, -5000f), ForceMode.VelocityChange);
jump = false;
}
}
TEST 3 (Tried other ForceModes)
void FixedUpdate()
{
if (jump)
{
rb.AddForce(new Vector3(0, 0, -5000f), ForceMode./*INSERT OTHER ForceModes*/);
jump = false;
}
}
TEST 4 ( I know, changing velocity directly is a bad thing, but seeing as what I want is the cube to move upwards at a certain speed, it made sense to try it )
void FixedUpdate()
{
if (jump)
{
var vel = rb.velocity;
vel.z += -5000f;
rb.velocity = vel;
jump = false;
}
}
It's been a long day, I'm sure I left some other attempts out.
Can someone explain to me why the cube is teleporting from Z=0 to Z= -2 (or some other value depending on the magnitude of the force)?
I can understand if it was something having to do with impact force not having enough time to change the velocity, but then why did the position change so drastically? And why didn't changing the velocity work, even though I know that is a bad way to go about it?