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
1
Question by ThiagoTejo · Dec 16, 2013 at 04:38 PM · positionvector3distancevector3.project

Vector3.Project, how to?

Hello, I'm trying to make my character to be placed on a rope when coming near to it. I've done everything now, the only problem, is placing the character on the exact X and Z location ( the Y position is the same ). The solution seems to be using a projection of my character on the rope, and then place the character on this projection, but I don't know exactly how to do this.

Here's the exemple: alt text

the rope's transform.position is in the center of the rope, And its transform.forward points where the rope mesh points. What I want is to get an axis from the rope, and then get the projection of my player on it. I read the reference about Vector3.Project but it isn't very clear. Can someone help me with this? Thanks in advance!

distance.png (4.9 kB)
Comment
Add comment · Show 4
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 flaviusxvii · Dec 16, 2013 at 04:47 PM 0
Share

I may have misunderstood your prior question. Do you just want the closest point on the rope to the player. Because that's a little different than what I suggested.

avatar image ThiagoTejo · Dec 16, 2013 at 04:51 PM 0
Share

Hmm, so how can i Achieve this, then? But I guess its better finding the projection of the player on a specific axis, because there's ton of other circunstances in wich i need something like this...

avatar image flaviusxvii · Dec 16, 2013 at 04:54 PM 0
Share

I was trying to clarify.. are you looking for the point on the rope that is closest to the current player position?

avatar image ThiagoTejo · Dec 16, 2013 at 05:01 PM 0
Share

http://answers.unity3d.com/questions/598010/whats-the-best-way-to-get-the-distance-between-two.html

Someone awsered there! :) I was looking for the X distance between two Vector3 in their current rotations, something like that. Thanks for your help too!

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Bunny83 · Dec 16, 2013 at 05:43 PM

It's pretty easy. You create a vector(V1) from your rope transform (blue dot) to your player (red dot). As normal vector you use the rope-transform-forward vector (i guess your line is along the forward vector). Since .forward is already normalized you don't have to normalize it.

then just insert V1 and as normal .forward into Vector3.Project and you get the vector(V2) from the ropeT (blue) to your desired point P (yellow).

Finally you just have to add the rope transform position and the projected vector V2 together and you get the world position of the point P.

An example:

 //C#
 public Transform rope;
 public Transform player;
 
 Vector3 ProjectPointOnRope(Vector3 aPoint)
 {
     Vector3 V1 = aPoint - rope.position;
     Vector3 V2 = Vector3.Project(V1, rope.forward);
     return rope.position + V2;
 }
 
 void Test()
 {
     Vector3 projectedPlayer = ProjectPointOnRope(player.position);
     // ...
 
 }

In one line it would be:

 // C# and UnityScript:
 var projectedPlayer = rope.position + Vector3.Project(player.position - rope.position, rope.forward);

edit

The above method could be generalized as extension method for the Ray struct:

 public static Vector3 Project(this Ray aRay, Vector3 aPoint)
 {
     Vector3 V1 = aPoint - aRay.origin
     Vector3 V2 = Vector3.Project(V1, aRay.direction);
     return aRay.origin + V2;
 }

This would allow you to project any point onto the given ray:

 var ray = new Ray(rope.position, rope.forward);
 var projectedPlayer = ray.Project(player.position);
Comment
Add comment · 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
0

Answer by robertbu · Dec 16, 2013 at 05:17 PM

You can get the position on the rope by intersecting the line that is the rope with a plane facing up at the correct 'y' height. Take a look at the Wiki Math3D functions:

http://wiki.unity3d.com/index.php/3d_Math_functions

In particular look at LinePlaneIntersection(). The plane normal with be Vector3.up, the plane point with be Vector3(0,y,0), the line point will be rope.position, and the line vector will be rope.up.

Note this will only work if you are guaranteed that rope exist at the specified 'y' height. If there is any chance that the player will be below or above the rope, then you can use Unity's mathematical Plane class and raycast from each end of the rope to generate the position and figure out if the rope exists at that specific height. Or you can just check the 'y' height of each end of the rope after doing the line/plane intersection calculation.

There are computationally more efficient ways to do this calculation, but unless you are going to be doing it many times a frame, probably not worth the effort to code them.

Comment
Add comment · 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

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

19 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

Related Questions

Return to Start Position 1 Answer

Distance won't work 1 Answer

Find position of point beetween two objects on certain distance 1 Answer

Enemy Jitters when i want him to stop a certain distance 0 Answers

Change the "y" of a variable 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