Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DJJD · Dec 13, 2017 at 02:02 AM · vector3distancefloat

Add a float to a Vector3 in a direction

Hi,

I'm trying to teleport my player to a distance in the direction of my mouse. I can check if my mouse is over a certain distance, and if so, then I want to teleport only to my maxDistance for my teleport (which is a float) in the Direction of my mouse. How do I know where to teleport too based on my current position, the mouse position and my float?

 if(Vector3.Distance(transform.position,Camera.main.ScreenToWorldPoint (Input.mousePosition)) > _dashDistance)
             {
                  // Teleport :D
             }

Thanks!

Joey

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
0

Answer by Bunny83 · Dec 13, 2017 at 02:22 AM

In your case you can simply use "Vector3.ClampMagnitude". Something like this:

 // vector from your current position to the mouse position
 Vector3 dir = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
 
 // clamp the "dir" vector to have a length between 0 and " _dashDistance".
 dir = Vector3.ClampMagnitude(dir, _dashDistance);

ClampMagnitude is basically the same as doing:

 if (dir.magnitude >  _dashDistance)
     dir = dir.normalized *  _dashDistance;

Just more optimised. Normalizing a vector makes the vector length to be 1 unit. The vector will keep it's direction. So multiplying a normalized vector by a certain length will change the vector length to the specified value. The length / magnitude of a vector is its Euclidean distance.

Comment
Add comment · Show 3 · 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 DJJD · Dec 13, 2017 at 03:19 AM 0
Share

I kind of understand why it should work, but it does not right now. When I click the button to do so, 2 things happen :

  1. I get to the mouse cursor anyway, bypassing the maxDist.

OR

  1. I go all the way back to somewhere very close to my inital Position?? I move all the way to somwhere far, I click the button and go back to the place I started the level ... (that is weird honnestly because I did not save my initial pos anywhere).

I did :

  if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
         {
             if(Vector3.Distance(transform.position,Camera.main.ScreenToWorldPoint (Input.mousePosition)) - 10 > _dashDistance)
             {
                 Vector3 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
                 dir = Vector3.Clamp$$anonymous$$agnitude(dir, _dashDistance);
                 transform.position = dir;
             }
             else
             {
                 transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 transform.Translate(0, 0, 10);
             }
         }







avatar image Buckslice DJJD · Dec 13, 2017 at 04:01 AM 0
Share

Is your game in 2D or 3D? If 3D, Camera.main.ScreenToWorldPoint() needs a Vector3 where the z direction specifies the depth of the point away from the camera. Also why are you subtracting 10 from the distance check than translating by 10 from the z otherwise?

avatar image Bunny83 DJJD · Dec 13, 2017 at 04:23 AM 0
Share

Look, if you take another look at your question at the top you haven't even mentioned how you move your object. So we can only make assumptions about your setup and how you use the code fragment you've posted.


"dir" is not a new worldspace position but it is the relative movement you should apply to your position. You don't need the if statement at all. You just do

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
 {
     Vector3 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     dir = Vector3.Clamp$$anonymous$$agnitude(dir, _dashDistance);
     transform.position += dir; // note the  "+="
 }


$$anonymous$$eep in $$anonymous$$d that this will move your object instantaneously towards your mouse position but not more than _dashDistance from the current position.


If you always want to "dash" the same amount in the direction where the mouse position is you can just use

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
 {
     Vector3 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     transform.position += dir.normalized * _dashDistance;
 }


Though i guess the first one is what you're actually after. So if the user clicks just 3 pixels from the current position he should only move those 3 pixels. But if he clicks further away than the allowed distance it should only move that distance.

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

90 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3(x,y,variable)? 0 Answers

Keep camera at certain distance from character when rotating 3 Answers

Return to Start Position 1 Answer

Make Navmesh agent follow closest object with tag 0 Answers

Check distance between many objects 3 Answers


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