We now adjust the velocity of the sphere, which PhysX uses to move it. If you know what you're doing adjusting velocity directly is fine. What we're doing is effectively applying an acceleration, just in a controlled way so we reach a target velocity. That's generic advice based on the idea that an instantaneous change in velocity is unrealistic. Isn't the advice to not adjust velocity directly? Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange) īody.velocity = velocity Controlling body velocity. Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange) velocity = body.velocity įloat maxSpeedChange = maxAcceleration * ltaTime Vector3 displacement = velocity * ltaTime //Vector3 newPosition = transform.localPosition + displacement //transform.localPosition = newPosition body.velocity = velocity īut physics collisions and such also affect velocity, so retrieve it from the body before adjusting it to match the desired velocity. Remove the displacement code from Update and instead assign our velocity to the body's. Vector3 newPosition = transform.localPosition + displacement We defer to the physics engine for collisions from now on, so remove the area code from Update. There's also soft-body physics, which is more complicated and unstable.Īdding the component is enough to turn our sphere into a physics object, provided that it still has its SphereCollider component as well. It's used for simulating the interaction between ideal rigid bodies, meaning that there is no deformation to worry about, which greatly simplifies physics calculations. We can use the default configuration of the rigidbody. We'll use the first approach to control the sphere, which means that we have to add a Rigidbody component to it. Second is the kinematic approach, which is to have direct control while only querying the physics engine to perform custom collision detection. First is the rigidbody approach, which is to have the character behave like a regular physics object while indirectly controlling it, either by applying forces or changing its velocity. There are two general ways to approach controlling a character in combination with a physics engine. Rather than implement that ourselves we'll make use of Unity's existing physics engine, which is NVIDIA's PhysX. But if we want our sphere to move around a complex 3D environment then we have to support interaction with arbitrary geometry. It made sense to explicitly program such a restriction because it's simple. In the previous tutorial we constrained our sphere to remain inside a rectangular area. This tutorial is made with Unity 2019.2.11f1. This time we'll use the physics engine to create more realistic motion and support more complex environments. This is the second installment of a tutorial series about controlling the movement of a character. Use a ProBuilder to create test scenes.Control the velocity of a rigidbody sphere.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |