Monday, August 1, 2022
HomeGame Developmentphysics - Unity Engine Motion Script Causes Shark To Fly

physics – Unity Engine Motion Script Causes Shark To Fly


Let’s recap some dimensional evaluation:

  • a Displacement measured in $m$, which is totally different from…

  • a Velocity measured in $frac m s$, which is totally different from…

  • a Power measured in $frac {kg m} s$

As a result of these are totally different portions, taking code for one and copying it into one other won’t yield the identical impact.

Let’s dimensionally analyze what you are attempting so as to add:

SharkCamera.rework.ahead * CurrentSpeed * Time.fixedDeltaTime

$$require{cancel}textual content{(dimensionless)} cdot frac m {cancel s } cdot cancel s = m$$

So that is producing a price in metres, so it is a displacement. A displacement shouldn’t be a velocity, so you’ll be able to’t add it to your velocity. What your code there does is maintain including an increasing number of to your velocity each body, so your object retains getting quicker and quicker.

If you wish to make the rate level in a sure route with a sure magnitude, simply assign it with =, do not accumulate it with +=:

Rb.velocity = SharkCamera.rework.ahead * CurrentSpeed;

Observe that we need not multiply by delta time – we’re not taking the velocity and integrating it over a time interval to make a displacement, we’re utilizing the velocity as a velocity, natively. Repeating the dimensional evaluation on this, we get $ frac m s$, which is precisely the unit we count on for a velocity.

Do this train of checking whether or not your items make sense anytime you are doing something with physics – it is an important debugger to identify whenever you’re writing code that is not sensible.


To assist this play extra properly with different forces, you’ll be able to as an alternative apply an acceleration towards your goal velocity, quite than setting the rate straight. That may appear to be this:

// Compute the rate we wish to have:
var targetVelocity = SharkCamera.rework.ahead * CurrentSpeed;

// Compute its distinction from our present velocity.
var deltaV = targetVelocity - Rb.velocity;

// Set a funds for a way a lot we are able to change our velocity this body.
var accelLimit = acceleration * Time.deltaTime;

// Scale our change in order that it doesn't exceed the acceleration restrict.
deltaV = Vector3.ClampMagnitude(deltaV, accelLimit);

// Apply the rate change.
Rb.AddForce(deltaV, ForceMode.VelocityChange);

You possibly can improve your acceleration parameter to make the controls tighter and extra responsive, or lower it to make extra gradual ramp-up.

You possibly can apply a number of different adjustments too – like taking a few of Rb.velocity.y and mixing it into targetVelocity.y, or scaling deltaV in order that your vertical acceleration change is damped relative to horizontal, or scaling your acceleration restrict by how intently the route of velocity change aligns with the shark’s ahead axis, and many others. Any of those strategies can assist you dial in the correct swimming really feel.


It’s also possible to simply get steady collision detection with MovePosition and maintain your present code, opposite to your assumption that that is unattainable.

Simply scan for collisions earlier than you progress:

// Compute size of a full transfer.
var distance = CurrentSpeed * Time.deltaTime;

// Sweep a capsule from the place we're to the top of the transfer.
if (Physics.CapsuleCast(
       rework.place - rework.ahead * capsuleLength,
       rework.place + rework.ahead * capsuleLength,
       capsuleRadius, 
       SharkCamera.rework.ahead,
       out RaycastHit hitInfo,
       distance,
       obstacleLayers)
) {
    // If the swept check hits one thing, cease only a hair earlier than the collision.
    distance = hitInfo.distance - 2 * Physics.defaultContactOffset;
}

Rb.MovePosition(rework.place + SharkCamera.rework.ahead * distance);

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments