#physics problem
1 messages · Page 1 of 1 (latest)
I believe your issue is in your update method.
Vector3 Xm = new Vector3(Input.GetAxisRaw("Horizontal"), rb.velocity.y, Input.GetAxisRaw("Vertical"));
Vector3.Normalize(Xm);
rb.velocity = transform.TransformDirection(Xm) * speed; /*
Is it the fact it creates a new vector3 every frame?
the pertinent problem: Vector3.Normalize doesn't modify the argument
it returns a new vector
so that isn't doing anything
therefore, every frame, your y-velocity gets multiplied by speed
you could fix that by doing Xm = Vector3.Normalize(Xm);
however, that would not behave very well
your vertical velocity would just kinda get stuck at a maximum of speed
I would suggest computing your X and Z movement separately from your Y movement
Read input into the X and Z components of a vector, normalize it if it's non-zero (otherwise you'll normalize a zero vector!), and multiply that by speed
then just copy the old y-velocity over
So basically have two separate Vector3's, one for my X and Z, and one for my Y?
Yeah, that'd be one way to do it. I wouldn't even bother making the "vertical" vector tho
maybe if there was more complex logic going on
the rigidbody already does gravity for you, and you aren't doing anything else funky with the vertical velocity
so I'd just compute the XZ vector and then do moveVec.y = rb.velocity.y
So what your saying is instead of using rb.velocity.y, put a 0. Normalize, and then set the value of the Y to rb.velocity.y?
Ok, I think I understand. What is moveVec just out of curiosity
it would be what you called Xm
I'll usually have something like
Vector3 inputVec = new(xInput, 0, yInput);
inputVec = transform.TransformDirection(inputVec);
Vector3 moveVec = inputVec * speed;
get user input, transform it from local space to world space (to account for rotation), and then compute a movement vector from that
again, Normalize doesn't modify the argument
it literally can't in this case, because Vector3 is a struct: thus, a value type
So just get rid of it?
even if the function changed the values in the argument, it wouldn't affect the original
no, you need to save the result...
look up Vector3.Normalize in the unity docs
look at the return type
Whatever I did, It just worked. Jumping needs a little fixing, but it works nonetheless