- Home /
Can I get a rigidbody's velocity and convert it to a fill value of an image?
I am currently in the midst of trying to create what would essentially be the equivalent to a Vertical Speed Indicator. I have two images created separately that meet in the middle and fill in the opposite direction. And I am trying to find/create a code for example that would scale the fill image as the gameobject goes forward/backwards & up/down.
The problem I'm running in to is while I know how to set the fill image based on a certain velocity ex. 'if (playerRb.velocity.y = 5) indicatorUp.fillValue = .25;' obviously that isn't actually taking the velocity and applying it.
Im not sure im understanding... Do you mean ```
indicatorUp.fillValue = mathf.clamp(playerRb.velocity.y/100,0,1)
``` That would make the indicatorUp.fillValue equal to the velocity of the player / 100 so it fits between 0 and 1 until it goes past 100/100 (or 1) and then it gets clamped to 1 for any higher values
Ok, that seems very helpful. Why did you divide it by 100 though?(edit, never$$anonymous$$d obvious why you divided it by 100 lol)
To clarify and try to be concise. Lets arbitrarily say that 20 is the max velocity for a given object. For simple math I'll say on the image that will be portraying the velocity of said object there will be four labels. So, with that in $$anonymous$$d, say the object is traveling at a velocity of 5, I would want that to set the fillvalue of indicatorUp to .25 so it will be 25% full. And obivously so on, velocity of 10 = .5... And hopefully that is clear enough.
EDIT: Also my apologies, fillAmount not fillValue.
Thank you! I took my socks off and did some public math. indicatorUp.fillAmount = Mathf.Clamp(playerRb.velocity.y / 20, 0, 1);
Answer by mf41z · Jan 14 at 09:21 AM
You'll probably need a max velocity value. Let's say you make the velocity fill a bar ( Like a health bar in a game ). So you could do something like this
private void Update()
{
Vector3 vel = GetComponent<Rigidbody>().velocity;
Vector3 maxVel = new Vector3(10f, 10f, 10f);
Image imageX;
imageX.transform.localScale.x = vel.x / maxVel.x * parentImage.transform.localScale.x;
}
I didn't test that and that probably wont work but it's something along those lines. You set the scale of the image depending on the velocity. You add a max velocity so the bar doesn't burst out of the parent. Max vel can be dynamic, just change it to watever you like during runtime as well. Did that help you?
Someone actually answered it in the comments using the mathf.Clamp() function. Which worked out really great but I appreciate the help. I'm not super sure I follow your code because it doesn't involve doing anything with imageX.fillValue.
Your answer
Follow this Question
Related Questions
How to add equal force across multiple platforms 1 Answer
Trying to set velocity to a gameobject without a rigid body 3 Answers
Best way to throw GameObject with mouse ? 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
rigidbody.velocity not working on x axis 2 Answers