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 Skwayn · Mar 20, 2014 at 02:46 AM · c#mathdistancevector3 operations

Set the speed of an object by its distance with another object

Hi everyone !

The title may not be clear so let me explain what I want.

I'm making a four players game, with a single camera. My camera is following a path of points I made in the editor and I want to set its speed so that it always stays right behind the last player. Players are in a corridor so they won't ever go too far left or right.

What I want is : to calculate the distance between each player and the camera, take the lower distance and set the speed with it.

The problem is I only want to know the distance player/camera on the forward axis of the camera, like this :

alt text

If I use Vector3.Distance() it'll say that P4 is closer than P2. But on the Z axis of my cam P2 is closer. That's what I want.

I tried to do it with some Cos like this :

 Vector3 v3_dirToPlayer = v3_playerPos - transform.Position;
 float f_angleToPlayer = Vector3.Angle (transform.forward, v3_dirToPlayer);
 float f_distToPlayer = Mathf.Cos (f_angleToPlayer) * Vector3.Distance (v3_playerPos, transform.Position);

But it didn't work well. The speed was good as the player kept moving forward but when moving left or right the speed went negative and really too far under 0 for no reason at all.

I'm asking for help. If someone has an idea please let me know.

Thanks !

cam_pb.png (5.4 kB)
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
Best Answer

Answer by robertbu · Mar 20, 2014 at 02:52 AM

A solution is to transform the four point into points local to the camera. You can use Transform.InverseTransformPoint(). Then you can just look at the 'z' distance to the point. For example, say you transform for p1:

 Vector3 p1_local = transform.InverseTransformPoint(p1.position);

Now the 'z' value of 'p1_local' will be relative to the camera both in terms of position and direction. If p1 was at the same position as the camera, then v1_local would be (0,0,0). The 'z' value will be how far forward (or back) of the camera the points is in the coordinates of the camera.

Comment
Add comment · Show 1 · 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 Skwayn · Mar 20, 2014 at 03:30 AM 0
Share

Well... works great xD thanks ! I didn't really get the function of InverseTransformPoint. I tried to use it doing difference between player's localPos and cam localPos so it didn't work and I gave up on this...

Thanks, you helped me a lot !!

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

20 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

Related Questions

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

Distribute terrain in zones 3 Answers

Decrease value at half the speed it's increased at? (using Vector3.Distance) 1 Answer

How to get the distance between two objects in feet/meter? 1 Answer

Getting the distance in a known direction 2 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