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
3
Question by Davste · Feb 12, 2012 at 06:52 PM · c#clampviewporttoworldpoint

Limiting player movement (Partially done)

I am currently using this code to restrict player movement along the horizontal axis, to ensure that the player stays within the camera view. Right now it works for the horizontal axis, but not the vertical axis, for some reason.

In other words, topB and downB values do not seem to be read correctly.

 void Update () {
         
         float distance = (transform.position.z - cam.transform.position.z);
         float leftB = cam.ViewportToWorldPoint(new Vector3(0, 0, distance)).x;
         float rightB = cam.ViewportToWorldPoint(new Vector3(1, 0, distance)).x;
 
         float topB = cam.ViewportToWorldPoint(new Vector3(0, 1, distance)).z;
         float downB = cam.ViewportToWorldPoint(new Vector3(0, 0, distance)).z;
 
         Vector3 pos = transform.position;
         pos.x = Mathf.Clamp(transform.position.x, leftB, rightB);
         pos.z = Mathf.Clamp(transform.position.z, downB, topB); //Not working well: 
         // topB is roughly the center of the screen while downB is well beyond the 
         // bottom of the  screen.
 
         transform.position = pos;
     }

Video describing problem: http://youtu.be/wfGoLZ2GV84

Why can't I make the camera just follow the player?

I have two players on screen and a camera which increases the ortographicSize as they get farther away from each other. At a certain point it stops stretching and needs to start restricting player movement instead.

Notes

he game is top down at an angle. I have been looking for help on camera.ViewportToWorldPoint, but I think I am not getting the dist (distance) right, since the camera is rotated at an angle, not straight down on the players (the cubes in the pictures below). Screenshot

Comment
Add comment · Show 5
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 luizgpa · Feb 14, 2012 at 07:29 PM 0
Share

Have you tried to use the real distance between the object and the camera?

 float distance = Vector3.Distance(transform.position,  cam.transform.position);
avatar image Davste · Feb 14, 2012 at 08:21 PM 0
Share

@Luiz It almost works :) It is a bit glitchy though because it also takes the X into consideration though. Any way I can remove that? I have tried distance -= (transform.position.x - cam.transform.position.x); With no success.

avatar image Davste · Feb 14, 2012 at 08:44 PM 0
Share

Nope, removing the X from the equation still isn't perfect.

avatar image luizgpa · Feb 14, 2012 at 09:07 PM 1
Share

humm, you could try something like:

 float distance = Vector3.Dot(cam.transform.forward, transform.position - cam.transform.position);
avatar image Davste · Feb 15, 2012 at 10:12 PM 0
Share

@Luiz Works! Can you put it as answer and explain in more detail maybe? :)

1 Reply

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

Answer by luizgpa · Feb 16, 2012 at 01:35 AM

If I understood your logic correctly you need the distance from the camera to a plane, parallel to the camera's projection plane, wich contains the player. One way to get this value is projecting a vector from the camera to the player into the camera's forward direction. This is accomplished by:

 float distance = Vector3.Dot(cam.transform.forward, transform.position - cam.transform.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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Mathf.pingpong 4 Answers

regular expressions 1 Answer

Video Scrubber Bar & Rewind using MovieTexture? 0 Answers

Limiting the rotation of x-axis 1 Answer

A node in a childnode? 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