• 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 wenhua · Feb 02, 2012 at 09:03 AM · displayoffset

Somethings is offset and shift from the original place

This fist map is using in big Mac Com with a bigger size. and second Map is display in normal computer . So why my way point is shifted and didnt display in correct order in normal computer, Below is my script. Some1 please help me check it up Please. THx

//mouseclick to add waypoints

if (Helicopter_Script.flightState == 0) { if (Input.GetMouseButtonDown(0)) { position = Input.mousePosition; if (position != prevMousePosition) { prevMousePosition = position;

             //Debug.Log("MouseClicked: mousePosition (x) - "+position.x + ", (y) - "+position.y);
             if ((position.x>=20 && position.x<=570) && (position.y>=150 && position.y<=780))
             {
                 if (helicopter.GetComponent( "Helicopter_Script" ).wayPoints.length == 0)
                     GameObject.Find("Home").renderer.enabled = true;
                 if (helicopter.GetComponent( "Helicopter_Script" ).wayPoints.length < maxWaypoints)
                 {        

                     points1.push(position);
                     linePoints = new Vector2 [points1.length + 1];
                     points1.ToBuiltin(Vector2).CopyTo(linePoints,0);
                     linePoints[linePoints.length-1] = points1[0];
                     if (line1 != null) Vector.DestroyLine(line1);
                     line1 = new VectorLine("Line1", linePoints , Color.red, null, 2.0, LineType.Continuous);
                     Vector.DrawLine(line1);

                     mapPosition.z = (position.y - 150) * (3300.656 + 1327.897) / (780 - 150) + (-1327.897);
                     mapPosition.x = (position.x - 20) * (824.0381 + 3285.686) / (570 - 20) + (-3285.686);
     
                     object = "p"+(helicopter.GetComponent( "Helicopter_Script" ).wayPoints.length+1);        
                     GameObject.Find(object).transform.localPosition.x = mapPosition.x;
                     GameObject.Find(object).transform.localPosition.z = mapPosition.z; 
                     GameObject.Find(object).renderer.enabled = true;

                     Helicopter_Script.wayPoints.Push(mapPosition);
                     Helicopter_Script.isNewWayPoint = true;
                     Debug.Log("numWaypont - "+Helicopter_Script.wayPoints.length +", mapPosition - "+mapPosition);
                 }
             }
         }
     }
 }
   
 //draw lines to waypoints

// pathPoints[pathIndex] = thisTransform.position; // pathLine.maxDrawIndex = pathIndex-1; // Vector.DrawLine (pathLine); // Vector.SetTextureScale (pathLine, 1.0);

Comment
Add comment · Show 1
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 wenhua · Feb 03, 2012 at 01:34 AM 0
Share

is there anyway to solve this,anyone?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Feb 03, 2012 at 03:59 AM

The screens may have different sizes, what could produce the difference. The only way to reliably convert mouse positions to world points (and vice versa) is through the use of some specific Camera functions.
Converting a 3D world point to a 2D screen point is easy:

   screenPoint: Vector2 = Camera.main.WorldToScreenPoint(worldPoint);

But converting screen points to the world is more complicated: the 3D conversion of a 2D point results in a line, not a point, thus you must decide somehow which point of this line to use.
In your case, the easiest way is to cast a ray from the mouse position and find the point where it hits the map plane. To do that, you must create an infinite plane aligned to the map, then use plane.Raycast and ray.GetPoint, like this:

   var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // create the ray
   var mapPlane = new Plane(Vector3.up, Vector3(0, 0, 0)); // create the map plane
   var distance = 0;
   mapPlane.Raycast(ray, distance); // get the distance from the ray start
   worldPoint = ray.GetPoint(distance); // return the map point hit by the ray

This code assumes that the plane is horizontal (normal = Vector3.up) and that 0,0,0 is a point of this plane (the plane created is horizontal and infinite, thus only the y coordinate matters in this case).

Comment
Add comment · Show 7 · 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 wenhua · Feb 03, 2012 at 06:05 AM 0
Share

those script are not attached to my camera,its attached to an empty gameOject(HUB). abit dont understand what you means. i also dun quit know how did my previous student display the map on the scene without any camera.. To me i guess is it a fix map or somethings. confuse

avatar image wenhua · Feb 03, 2012 at 08:00 AM 0
Share

now i find that its using $$anonymous$$apCam to display the scene. $$anonymous$$apCam is using Orthographic size 800 far 5000 looking toward the terrain. its attached with a Cam Follower. somehow i move the $$anonymous$$apCam's x coordinate, slowly the offset will get to normal but the helicopter icon should be able to move along the line, now the problems is the icons is not moving. think is screen resolution.how sia

avatar image aldonaletto · Feb 03, 2012 at 01:17 PM 0
Share

The camera shows the map, and sets the relationship between world and screen coordinates - that's why you must use the camera functions to convert world->screen and screen->world. In your script, this conversion is being made with these equations:

 mapPosition.z = (position.y - 150) * (3300.656 + 1327.897) / (780 - 150) + (-1327.897);
 mapPosition.x = (position.x - 20) * (824.0381 + 3285.686) / (570 - 20) + (-3285.686);

But this only works for a given camera position and screen size - any changes in these setting result in position errors. You should create a function to do the conversion, like this:

function ScreenTo$$anonymous$$ap(position: Vector2): Vector3 {
  var ray = Camera.main.ScreenPointToRay(position); // create the ray
  var mapPlane = new Plane(Vector3.up, Vector3(0, 0, 0)); // create the map plane
  var distance = 0;
  mapPlane.Raycast(ray, distance); // get the distance from the ray start
  return ray.GetPoint(distance); // return the map point hit by the ray
}
Then you could replace the two lines above with the following:
 mapPosition = ScreenTo$$anonymous$$ap(position);
avatar image aldonaletto · Feb 03, 2012 at 01:23 PM 0
Share

I don't know this Vector class, but I suppose it uses screen coordinates. If this is true, you must convert the helicopter's world position to screen coordinates to draw it at the right position. You can do it using this:

 screenPoint = Camera.main.WorldToScreenPoint(helicopter.position);

where screenPoint is a Vector2 that will receive the screen coordinates, and helicopter.position is the helicopter's transform.position.

avatar image wenhua · Feb 06, 2012 at 08:39 AM 0
Share

indeed its using screen coordinates, but i have no idea how to convert and where to place it T_T as its too complicater. but i will try to understand as much as possible

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

how can I display a variable as a GUIText 5 Answers

Mesh UVs won't change positions 1 Answer

Applying offset to a texture via script 1 Answer

way to get all object with a certain component/script attached 3 Answers

Decreasing footstep length when key pressed 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