Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ramonfr · Oct 04, 2012 at 05:31 PM · vector3mathf

Math question

Hi there, I got a problem relacted to math.

In my game, the player is able to kick a ball.

The force of the kick is set by how long he keeps the mouse button pressed down, and the angle of the kick is set by how close the mouse pointer is to the player when he kicks.

Here comes the problem:

If I get the distance from the player to the ball with the function Vector3.Distance(player.position, ball.position), I'll get low numbers when they are close to each other and higher when far.

The kick will have a force 1 when they are near and force 9 when they are far, for example.

So, I need the contrary; 1 when far and 9 when near.

Do you know any solution to this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Dragonlance · Oct 04, 2012 at 05:53 PM

Define the max Distancewhere it is still hitting with force 1.

Lets say

 int minForce = 1;
 int maxForce = 9;
 float maxHitDistance = 1.8f;

then get the distance

 float distance = Vector3.Distance(player.position, ball.position);

then check the force factor

 float factor = (maxHitDistance - distance) / maxHitDistance;

If this is negative, the player is to far away...

If this is positive, you have a linear mapping with this factor from 0.0f to 1.0f. While distance 0.0f is equal 1.0. And if distance = maxHitDistance then it is 0.0f

 int force = 0;
 if(factor < 0.0f) {
     int maxDistanceForceBonus = maxForce - minForce;
     force = minForce + Mathf.Round(factor * maxDistanceForceBonus );
 }

We did it =) force is not lineary mapped from max distance to zero distance between maxForce and minForce in integer steps.

If you want it float, then change it to float and do not Round.

Have fun!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dragonlance · Oct 04, 2012 at 06:57 PM 0
Share

Thanks for the thumbs! And sorry for the code formatting. This somehow went wrong.

avatar image fafase · Oct 05, 2012 at 05:25 AM 1
Share

Hooray I make it look nice but I feel one detail is missing.

Linear attenuation goes like this:

 var d$$anonymous$$:float=$$anonymous$$value;
 var dmax:float=maxValue
 var dist = Vector3.Distance(from,to);
 var result:float;

 if(dist<=d$$anonymous$$)result = 1;
 else if (dist>=dmax)result = 0;
 else
    result = (dmax-dist)/(dmax-d$$anonymous$$);
avatar image Dragonlance · Oct 05, 2012 at 06:11 PM 0
Share

Yes but i am doing inverse linear attenuation with an offset, mapped to an integer range just like he wanted.

avatar image fafase · Oct 06, 2012 at 11:33 AM 0
Share

As long as he got what he wants, fine to me. Well now, there are both ways for everyone.

avatar image ramonfr · Oct 18, 2012 at 05:14 PM 0
Share

Finally I could test it, but I found a trouble.

Yes, the number grows when the objects are near, but it grows from negative numbers up to 0. When applying the force to the Y axis, the object wont go high, because the force is negative.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How the heck does Mathf.Lerp work? 2 Answers

Velocity to 2D rotation (angle from velocity vector with atan2) 1 Answer

managing floating point rounding 1 Answer

How can I have a distance between 2 points = 0-1 (min = 0, max = 1) and in between is a decimal? [C#] 2 Answers

How do I programmatically specify direction from a given vector? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges