Search Unity

Isometric ScreenToWorldPoint fine tuning.

Discussion in 'Scripting' started by Ziron999, Sep 25, 2017.

  1. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EditorMenuInputManager : MonoBehaviour {
    7.  
    8.     public GameObject BasicBuildingBlockTransparent;
    9.     public GameObject BasicBuildingBlockSingle;
    10.     public List<GameObject> ShipBlockList = new List<GameObject>();
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         /*Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    15.         if (Physics.Raycast(ray))
    16.         {
    17.             Vector3 point = ray.GetPoint(1);
    18.             point.y = 0;
    19.             BasicBuildingBlock.transform.position = point;
    20.         }*/
    21.         var worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    22.         worldPoint.x = (int)worldPoint.x + (int)(worldPoint.y);
    23.         worldPoint.z = (int)worldPoint.z + (int)(worldPoint.y);
    24.         worldPoint.y = 0;
    25.         //var v = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
    26.         //var worldPoint = Camera.main.ScreenToWorldPoint(v);
    27.         //(int) faster then convert.toint32 required to snap position...
    28.         //worldPoint.x = (int)worldPoint.x;
    29.         //worldPoint.y = 0;
    30.         //worldPoint.z = (int)worldPoint.z;
    31.         BasicBuildingBlockTransparent.transform.position = worldPoint;
    32.         if (Input.GetMouseButtonUp(0))
    33.         {
    34.             ShipBlockList.Add(Instantiate(BasicBuildingBlockSingle));
    35.             ShipBlockList[ShipBlockList.Count - 1].name = (ShipBlockList.Count - 1).ToString();
    36.             ShipBlockList[ShipBlockList.Count - 1].transform.position = BasicBuildingBlockTransparent.transform.position;
    37.         }
    38.     }
    39. }
    40.  
    So far This is what I've come up with my math for y is incorrect though.
    When you move towards the top of the screen there is a gap between the object and where it is located and it gets bigger the further up the screen you go with the mouse. This does appear to be completely a math problem. I don't know how to calculate y from the mouse correctly due to the isometric view.
     
  2. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    to be more specific for those who may be confused:
    1. worldPoint.x = (int)worldPoint.x + (int)(worldPoint.y);
    2. worldPoint.z = (int)worldPoint.z + (int)(worldPoint.y);
    ^ those are the 2 lines i believe are wrong...it shouldn't just be + y...but i don't know what it should be or why
    The camera is rotated to do the isometric view which is why this problem even exists. Somehow the y needs to be calculated to compensate...

    after messing around a bit i just did worldpoint.y*1.2f and it's hard to tell there is even a problem now but still would like a more exact solution...oh well...
     
    Last edited: Sep 26, 2017
  3. ohbane

    ohbane

    Joined:
    Jan 14, 2013
    Posts:
    7
    @Ziron999: due to the isometric projection. it is not 1.2, but 1.21. i strongly suggest that you read up on isometric projection or else you end up having weird results and bugfixes that cause even more game breaking issues, see https://gamedev.stackexchange.com/questions/125590/calculate-length-of-isometric-line and bear in mind that there are different "isometric" projections, in the common sense, i.e. "true" isometric projection, so check the exact angles you're using, see https://en.wikipedia.org/wiki/Isometric_video_game_graphics