my code:
var speed = new Vector3(15f,0f,0f);
rigid.AddForce(speed, ForceMode.VelocityChange);
I add force only in x, but rigid.LinearVelocity.y != 0 and rigid.LinearVelocity.z != 0.
my code:
var speed = new Vector3(15f,0f,0f);
rigid.AddForce(speed, ForceMode.VelocityChange);
I add force only in x, but rigid.LinearVelocity.y != 0 and rigid.LinearVelocity.z != 0.
ForceMode.VelocityChange imposes a change in velocity by the amount specified. So your 15, 0, 0 is saying you do not want to change the y or z velocities and you will add 15cm/s/s to your current x velocity. It is not saying set the velocity vector to 15, 0, 0. So any pre-existing velocities in the y and z directions will remain.
You can either override the rb velocity directly, which is generally not recommended (you are effectively ignoring physics and setting the velocity):
rb.LinearVelocity = new Vector3(15f, 0f, 0f);
or you can subtract the existing rb.LinearVelocity from your target velocity and use that with ForceMode.VelocityChange:
vector3 targetSpeed = new Vector3(15f, 0f, 0f);
vector3 delta = targetSpeed - rb.LinearVelocity;
rigid.AddForce(delta, ForceMode.VelocityChange);
Without seeing a simple replicator of your problem it is difficult to comment further. I would recommend you try to construct such a replicator as I often find that when I’m doing that I discover the answer to my problem.
I think that is what OP is questioning about… “Not wanted to change y&z velicities but it is changed”
Any I also agree that it is needed to make sure to check the motion of rb before adding the velocity.
so I want to konw why the rb.LinearVelocity.y be change… because physics materrial or my rigidbody setting?
So there are two levels of answer here:
Firstly, as I pointed out, you say you are using
var speed = new Vector3(15f,0f,0f);
rigid.AddForce(speed, ForceMode.VelocityChange);
Which is merely adding to the x component of the velocity, it is not setting the y component to zero, so any accumulated velocity in y will go unchecked. This may be due to impact with other physics objects, interaction with ground colliders in an unexpected way, gravity generating forces with non-horizontal ground, or some other code that you have forgotten about. This is why I said create a simple replicator and slowly add complexity until you find the problem.
Secondly, you should be aware that it is possible to accumulate displacement even when manually setting velocity to zero, if you have additional forces continuously acting on the rb. Although you may be setting the velocity to zero on each fixed update there will still be some velocity as a result of this.
NOTE: I do not think this is likely to be your problem.
The actual calculation sequence will be like this:
In FixedUpdate you set the velocity to 0, the physics step is then processed, resolving forces/gravity to produce a new velocity at the end of the physics step, which is reported out. Because of the way PhysX works (using the semi-implicit Euler method), displacement is calculated based on that final velocity. Generally this error is very small.
thanks, I will change value to fix it.