Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

I need help understanding this unity c# script?

I need help understanding this c# script. 

My first question is what exactly does the, rb.GetRelativeVector(vector2.down) * 2); do>

what is GetRelativeVector?

why is it passing a vector.down and why is that being multiplied * 2?

My second question on the scripts is why is the rb.velocity.magnitude being divided by the speed? what does that so?

I will greatly appreciate anybody being able to explain this to me in the most simple way possible. 

Attachment image

1 Answer

  • 2 months ago

    I have more questions than that.  This is an instance method that uses and modifies instance fields (rb, transform, movX, movY, acceleration, speed) and doesn't have a comment block to say what it does.  The method name "fixedUpdate()" isn't much of a clue, either.

    It computes a local variable "dir", but never uses it.  Why?

    Who knows what the class that this method belongs to is or does?  Who knows what the fields represent?  Yes, I see "speed" and "acceleration", but speed and acceleration of what?

    I don't have any Unity experience, but I can tell you that a dot product of an unknown vector with a unit vector finds the magnitude of that unknown vector times the cosine of the angle between the two vectors.  That's the component of the unknown vector in the direction of that unit vector.

    The component in the left-right direction (velocity dotted with a transformed "right" vector) is used to rotate the rigid body "rb".  The component in the up/down direction is used to change the speed of the body.  That's something close to real physics where the force in the direction of motion changes speed and the force perpendicular to the direction of motion changes direction only.

    The rotation part is murky.  You need to know what "speed" is.  The speed of the rb object is rb.velocity.magnitude, so "speed" must be the speed of something else. 

    What that will do in the "thrustForce" local variable seems like making the force on the rigid body "rb" equal to -2 times the vertical velocity.  (It's -2 instead of 2 because the dot product with a "down" vector is used to get the downward velocity compoent, but the 2x scaled version of that is applied to the "up" vector. 

    A force dependent on velocity only, and directed opposite from it, is a frictional force.  This some sort of weird friction that only works int the direction that the "rb" object is pointing (whatever its "up" direction is.)

    No, I don't know why it's 2 instead of 1.5 or 3 or something else.  This is a "magic number", apparently some sort of coefficient of friction.  This is something that many experienced programmers would call a "code smell".  Even if it's right, it looks wrong.

Still have questions? Get your answers by asking now.